My layout will have nested weights and will be in complex view-pager structure. Thus i want to do everything dynamically instead of creating them in xml file.
So what i wonder is, does creating dynamically improve performance a lot? Especially in the conditions above? I will be still using nested weights but this time the phone will not have to deal with xml parsing. Or Should i just ignore and do everything in xml?
Thanks in advance.
Creating your structure dynamically is a good idea specially if the user can add objects a lot of time (or you in the future when you add new fonctionalities).
If your layout is fixed, maybe it will be easier and it will load fastier in xml.
For an example, in a note application, you have no choice to add all element programatically.
Related
I have an activity with a scrollable view. I also have containers that all have the same structure - consist of imageButton and also several textViews that serve as a description. The whole thing looks like that:
The problem is that there are around 30 elements with the same structure and when each one of them has so many textViews I get the warning -
Layout has more than 80 views, bad for performance.
The reason why I have so many views is because of the different formatting of the different words. How would it be possible to reduce the views and still get the same formatting?
P.S. I already read that how to resolve "Layout has more than 80 views, bad for performance"? . But this doesn't help me, since all the information that I have, should be static and always visible.
You will want to set up your layout using a RecyclerView.
If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView.
Android Developer Guide has some pretty good documentation on how to set this up. Also, here is a video tutorial on RecyclerView by SlideNerd. He has a pretty good series for android developers.
In addition to what J. Jefferson said about RecyclerView. As you mentioned in your initial post:
The reason why I have so many views is because of the different
formatting of the different words. How would it be possible to reduce
the views and still get the same formatting?
You can do different text formatting without creating a separate textview. Have a look at the following https://stackoverflow.com/a/41953808/8312634
I have the following problem:
Download complex xml-layout with many views from the server while running the application, dynamically attach it to app and display the necessary elements with ability to work with them.
I learned that at the moment this is not possible, at least there are no simple ways to do this
(https://stackoverflow.com/a/1942224/1956299).
I am wondering is it possible to parse downloaded xml-layout for my self and create the necessary ui elements programmatically? Considering layout can have many different elements with child views etc.? If yes, how it would be better and easier to do this?
There is only solution to do this
Read Xml By XmlPullParser line by line get the View Tag from incoming layout file,View attributes and create View Dynamically by java code
i have a project HomePage with 4 ListFragments,
i create also 4 xml layouts of the listfragments,
there layout is the same so
if the same should i use only 1 layout for 4 ListFragments?
is it a good performance practice using 1 xml layout with listView
on 4 ListFragments Clases? each Fragments class there deference only
is from sortOrder in Cursor, so its ok to use only 1 xml to enhance or to less implementation of other objects or less ccreating xml can make or enhance the performance speed?
It is a good practice to reuse a layout when possibility because it allows you share as much XML as possible within your application.
Each layout file that you create needs to be added to your resulting APK. You can see that by reusing layout files, you can save the amount of layout files that end up in the APK.
However, there is no performance impact. Your code would not run faster if you were to re-use layouts.
You can get some more information on how to reuse layouts here: http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/.
I have a listview where the items contain different amount of child textviews.
min 5
max 20
i've tried both ways and I only have the vm to test my apps on so I can't really tell any difference performance wise.
but what's the best way to do this?
should I create the 20 textviews in my xml and just hide the ones I don't use?
or just create and add new textviews everytime, resulting in no "Ghost views"
Issues to realise:
Code cleanliness-> Doing anything more than basic layouts in code can get very messy, very fast.
Code re-use-> It’s extremely easy to inflate an XML layout into a specified view
with one or two lines of code
code Performance-> Creating the objects necessary for an in-code layout leads to unnecessary garbage collection. As per the Android Designing for Performance article, “avoid creating short-term temporary objects if you can.”
Attribute availability-> Defining Views in an XML layout exposes attributes that are not always available by object methods.
Possible disadvantages:
It make take more time to do an XML layout versus defining the layout in code, especially if there are only one or two interface elements that need to be changed.
After thinking about what I want to accomplish, it makes sense for me to use XML layouts for the dynamic view changes I need. It would be more than just a few lines of code to do my layout changes without utilizing XML layouts.
Now you can decide according to your requirements.
i got the following "problem".
I want to have an activity thats shows me the standings of some teams at a specific gameday.
therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.
Whats the best way to create this activity?
assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml
assemble the layout during runtime, add ids, update via ids
assemble the layout during runtime. on update remove old views and create new ones
assemble the layout during runtime. on update restart the activity
just whant to know which one is the best. or is there any other way to achieve that
thx Cheetah
If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.
In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.
Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.