I just started learning Android development and I read there was basically 3 main approaches to ‘build a view’ which are :
- Java-based: Use Java to define Strings, lay out window, create GUI controls, and assign event handlers. Like Swing programming.
- XML-based: Use XML files to define Strings, lay out window, create GUI controls, and assign event handlers. The Java method will read the
layout from XML file and pass it to setContentView- Hybrid: Use an XML file to define Strings, lay out window and create GUI controls. Use Java to assign event handlers
What are the benefits and limitations of these 3 different approaches ?
Which one should be rather used by a beginner or a confirmed programmer ?
I’m not asking for subjective answer here ( before being flagged 🙂 ).
There must be some facts that make these approaches different from one another (speed, maintainability, readability…)
XML-based is like using CSS for a webpage. Using XML separates concerns neatly into the MVC (Model-View-Controller) pattern. If everything is specified in XML, then your activity can utilize different layout files for different screens, and the presentation of these elements can easily be updated by just changing to new XML files. This is good software development practice and greatly helps when it comes time to redesign or reuse components. In some cases you may still need to dynamically set some things in Java, but you should try to put all presentation-related stuff in the XML files.
The event handlers should still be set and defined in Java, in my opinion. That is not related to presentation and thus does not belong in the XML files. I do not use the onclick XML attributes. Also, if you set it in the onclick attribute, you can break the connection if you refactor the method name in Java but forget to update the XML file.