Using XML in Android for development - android

Hi,
How to use XML code instead of Android code to be used regarding UI development? In what manner XML is different from ordinary Android code?

Basically, if you've static elements in your layout, it's better to use XML. If your layout has a property to change itself dynamically, then it's better to go for Java. When it comes to usefulness as such, using XML is a bit better because you can easily re-configure your XML when compared to the dynamically running code. Finally, designing is a matter of choice and opinion.

Its much easier to develop UI using XML than by JAVA code mainly due to visual Drag-and-Drop feature. But by XML way of laying out views is Static in Nature but by Java code it can made to work during Runtime(may be adjusted to different conditions). So it is recommended that part of the layout which you think, undergoes no change in time should be implemented by XML and vice-versa by JAVA coding. Also static UI by XML can be overrided by JAVA cod during runtime as per requirment.

Read this ,you will get your answer :
http://developer.android.com/guide/topics/ui/declaring-layout.html

Related

Is there any performance difference between defining layouts in XML and writing the same in java code?

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.

Android GUI XML vs Code

I have a short question according to creating GUIs in android. What way is better xml or coding?
I always read that xml is way better but imagine you have a scrollview.
Inside the scrollview is a relativelayout. Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. The number of rows can vary.
Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created?
Each has its pros and cons. Just to name a few:
XML
pros -> fast GUI development, keep code clean
cons -> static
Dynamic (code)
pros -> able to react to runtime conditions
cons -> more code, which means poorer maintainability and potentially buggier
If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). If your components are static XML may be best.
Dynamic content is, of course, added dynamically. So your example would require some java code. You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic).
If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml.
(One way to minimize code duplication within xmls' is with the usage of styles and themes)
I agree with the above. XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. your code will be based on XML elements but XML files will be independent. hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily.
Why you do not use a ListView instead of a ScrollView.
It will be simplier to implement and performances must be better with it.
Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows.
You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need !
Good luck.

Memory friendly : View in xml or java class?

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.

How do I get about adding something to my Android Layout?

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.

Which is best, Java or Xml for developing android apps

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

Categories

Resources