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.
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.
Is there any performance difference between layouts done in code vs XML layouts in android?
Yes. XML layouts need to be loaded, parsed and inflated to generate the View. Behind the scenes the LayoutInflater does exactly what you would do when writing layouts through code, but with the overhead mentioned before.
Here is an interesting article on this topic, which covers View generation through code, also it is about a library written in Kotlin: https://nethergrim.github.io/performance/2016/04/16/anko.html
But even though there is a performance win, I would not recommend to write layout in code for the following reasons.
You couple your layout and your business logic. That's always bad.
You can't use the features of the AppCompatDelegate (loading Views for the latest Androind version. E.g. an AppCompatButton instead of a normal Button.
As far as i know, i can make one difference. Please correct/enhance my answer if possible.
For XML based, In compile time, the ID generated and stored in R file which we use to refer to that any particular layout(like TextView, Button etc.) inside our code. As the reference ID is getting generated at compile time, so at run time this overhead is not there and it is faster.
In code based, all things done at run time which makes the app slow. It is not that much visible if small number of layouts are there. But if you are creating a lot of Layouts pro-grammatically, then you may realise the slowness in your app.
Its hard to imagine that using XML would ever be faster.
Android SDK has a lot of performance tweaks to make loading and parsing XML fast. in recent times, if you use data binding to replace findViewById this dramatically improves performance as well as each call to findViewById parses the XML tree. Where as databasing will only parse the tree once.
Also, using include in XML allows for reuse of Views which improves performance especially in cases where you would have large object churn. There is all sorts of I eternal caching and pooling of objects as well so it's possible that inflated objects are cached/pooled and cloned for subsequent use reducing the overhead on using XML. I know this is definitely the case for Drawable assets hence the need to call mutate if you ever plan on modifying one.
There are scenarios where code would be slower, with every View you create and add to your layout, you will trigger a measure and layout pass, so if you try to build a complex, deep nested layout using code at runtime, this would take a lot longer to load.
There are other factors to consider, like:
styling is hard if not impossible to apply at runtime.
code with views created complete runtime mY be complex, hard to maintain and bug prone.
You can use ConstraintsLayout to flatten your views hence avoid writing custom ViewGroups if performance is an issue.
XML allows use of the editor to preview and debug your layouts.
If the ViewGroup you want to create is simple, or simply want to create a single View and insert it into a ViewGroup, doing it in code is probably hard to beat by any criteria.
If you want to do something a lot more complex, using XML and all its features like ViewStub, includes, DataBinding is probably the way to go. I have noticed that inflating Views can be visibly slow, if you do it a lot, hence the ViewHolder pattern, but if you're careful, it's hard to justify ever building Views and ViewGroups in code for any thing but simple layout additions.
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.
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
I'm making a new application and its basically filled with information about Warcraft.
I have similar apps on my phone that have similar information and when I looked inside their .apk they only had like 10 layouts.
The app that I am making already has 5 layouts and it seems like I will need about 50-60 layouts.
So now my question is it normal to have that many layouts? Or do I have to learn to make one general layout and keep reusing it? For example, like if I need to display information about a topic for instance the classes in warcraft which are 10 different classes with 2-3 different guide pages on average for each class, would I need to make a different layout for each page or is their a better way of doing it?
I would really appreciate any input/suggestions.
What I recommend is having one layout for every type of screen (basically one per Activity) and use Java to fill in all of the info. Use getResources().getString(int id) and pass something from R.string. That means you need to keep all of your information in a strings.xml file in your values folder (located in /res/values). List all of your views in the layout XML file and then find them by ID and set their values.
It's best to keep all of your string resources in a separate XML file and not hard code them into the layout (otherwise it's a pain to replace every instance of a word you realized you misspelled or something). If you don't know how to write XML, that's okay since there are tools in Eclipse, but I HIGHLY recommend learning it.
You can re-use layouts without any problems. In fact for maintaining the code it is a very good idea to do so. No one wants to maintain 50+ layouts and associated code.