I am a complete newb to Android and have seen tutorials that show widgets being added via XML and also via Java. I want to know what industry experts prefer and why. The author/narrator seems to prefer XML saying it gives more control but I wanted to get feedback from veterans.
With XML Tutorial
Without XML Tutorial
Thank You and again I apologize for a complete newb question.
XML layouts are very easy compared to java code. The coding also very less than java.
You can prefer any of these two, but all authors prefer XML because its easy.
Please read information available at this link, you will get an idea.
XML Layouts
Declare UI elements in XML (most
common and preferred)
Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses,
such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.
Instantiate layout elements at
runtime (programmatically in Java code)
Your application can create View and ViewGroup
objects (and manipulate their properties)
programmatically (in Java code).
Advantages of : Declaring UI in XML
• Separation of the presentation from the code
that controls its behavior
You can modify UI without having to modify your
source code and recompile
For example, you can create XML layouts for
different screen orientations, different device screen
sizes, and different languages
• Easier to visualize the structure of your UI
(without writing any code)
Easier to design/debug UI
Visualizer tool (like the one in Eclipse IDE)
Advantages of : Instantiate layout elements at runtime
• While showing dynamic data
When your application need to show dynamically some information for example loading title from web then you need as many text-view per title and you are not sure at design time about this at that time this can be preffered
You can make your application design both ways and both will help you the same,It will depened on your requirement whether you require to create it at runtime or not,Anyways I think while you can make separate XML file why need to do it in your Activity java file and get more complexity in code unnecessarily.
XML layout method is very easy, fast and easy to visualize. You can do a paper-pencil work first and simply do it in XML.
XML layouts are easy to manage - you can change the caption, position, look & feel, size, colors etc. in the XML layout, without altering a single line of the Java Code. The App will generate the Java Code for the layout from your XML file.
XML layout does not compell that you should write the whole XML codes. Instead, you can use a GUI editor and you arrange the controls on screen, and it will generate the XML for you. There are lots of GUI design tools. Eg.: http://www.droiddraw.org
Now, you can go for Java Code UI layout only if you cannot determine the layout at the time of writing the App. Say, you are designing an Android App for Web Designers - An app to create HTML Forms and create the HTML code (a simple HTML editor tool), then you are unable to know which type of layout the user is going to do. At this point, the better option is choosing Dynamic Layout (or Java Code layout)
Almost every Android programmer chooses to do layout in XML. That's the beast and easiest way to do it.
Related
I have a requirement where different views are to be shown on the same screen depending on the requirement. Which approach is better adding respective XML files or writing in java code by using TabActivity class object?
Short answer, defining your layout in code is better for performance than using XML.
Inflating an XML layout involves doing the same work as creating the layout in code, however you also need to parse the layout file (at least the first time it's used) which adds work compared to doing it in code.
HOWEVER, for the majority of use cases the performance hit is not noticeable, and the simplicity of defining an XML layout outweighs the performance benefits of doing it in code.
IMO, Writing layouts file is a better option.
Since, Writing layout will let the view load on compile time.
Whereas, While creating views pro-grammatically leads to slight increase in Runtime operations of the app.
Code written in java means dynamic generation of views are faster than xml rendering as the views increasing. Xml prefers as it is bit easy to implement the design using xml but it gets tough to handle when we use java to create dynamic views.
In your case if you have requirements to create dynamic view then you can create with Java.
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.
The advantage of using XML as much as possible is that all the Android tools provide support for this style of programming (UI editors, etc.). If you do everything in code, you're on your own. (Want to see the effect of a change to your code-based layout? Build your app and run it in the emulator. With an XML-based layout, you can preview it right in the XML editor where you are making the changes.)
Regarding event handlers, the main advantage of declaring them in XML (e.g., with an android:onClick property) is that you don't have to declare the event handler classes. The advantage here, though is not particularly strong, and I often use what you describe as a hybrid approach.
I'd advise beginning Android programmers (whether experienced with other programming or not) to gain a strong foundation in the XML style that Android pushes.
I am starting with development in android apoplications, but not sure about the creation of views.
Which one of the two is more preferable in terms of memory-consumption for creation of views : XML or Java Class ?
Can you please suggest which approach is more memory-friendly either XML or Java classes for creation of view ?
There is ultimately Java behind the xml..
so both are same as creating from Java or using xml.
XML help us to customize Views quickly and drag & drop graphically.
The Layout tricks actually works. (For example: Here is an illustration to optimize the memory consumption.)
If you are just starting with the development and you are new to android..go with XML its easier to understand and experiment with.there are a lot of tutorials around.
Java classes are usually used to set view dynamically..so i would suggest start with XML and move to simple Java tutorials.
with memory friendly part(i dint get what you are trying to say) if its about the load time or the CPU usage.its non the less same.
For the purpose of memory-consumption XML Views are best to use but if you want to make your View Dynamic as you receive data from the Server or from any other resource at run time Then you can use Java for creating those Views.
I have know core java, I want to develop android apps, in few sites I saw apps in XML in few Java.Which is best and easy to use Java or XML?
I'm assuming you are talking about the UI, not the complete app:
For everything statically I use XML, because it is easy to find in the structure of your project.
Some parts you want to create dynamically and you have no other choice then to use Code. Be smart, in this, so if you have to add several Views that look the same do this
Make an XML with your views
In the loop where you are adding the several Views, inflate this xml, set your id's etc, and add them
You can have all the basics, styles etc in your XML, and still add stuff dynamically.
Maybe you want to check out some of the hello world code?
You can't build apps in XML. You use XML to define the UI and a few other things, but the logic of the app itself has to be written in Java.
Android development is in Java, but when you declare the layouts (where the buttons and so are going to be on the screen) you do that in XML.
So you have to use BOTH anyways.
XML for layout
JAVA for programming your app
I am new to Android.
I wish to know how to design a screen by using java in Android.
Design via xml and design via java which one is the best?
Design via xml is easier to maintain. For example, if you have to change something in your UI you just change it in one xml file instead of looking for it in all your source code.
On the other side, in java you can do some staff more quickly. Let's say you need to display a matrix of button. In java you can create and display them with a loop instead of creating them via xml.
To sum up, it depends on what you have to do. If you need to design more or less static UIs, opt for xml and use Java only to edit them at runtime.