Should I create child textviews statically or dynamically? - android

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.

Related

Reducing layout views in an Activity for better performance

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

Creating Xml vs Creating Views Dynamically

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.

Parse android xml layout and create views from it programmaticaly

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

best practice for using same xml layout or use single one in fragment listview in android?

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/.

How to list all dynamically created views in an activity?

I'd like to see all the dynamically created Buttons, TextViews, etc before (or even after) setContentView() shows them on the screen. I have a loose understanding that this relates to the Context and the Activity, but concretely I don't know where these dynamic views exist / how they are put together at runtime.
And if they are shown at runtime is there a way to list all of them?
LinearLayout layout = new LinearLayout(this);
layout.addView(button1);
layout.addView(button2);
layout.addView(button3);
setContentView(layout);
The buttons SHOULD exist somewhere (I am so sure they exist in the Context!!) but there is no way (that I have found) of locating these dynamically created views.
Please and thanks.
In general, if you are going to need to display these kinds of things on the screen, then you should keep a reference to them somewhere. This is a common pattern, if you dynamically create a button, you need to stuff them in a List (for example). You shouldn't need to get all the views if you program like this, and even if you easily could (typically in GUIs, you can, either by reflection, or something equally tacky mechanism) it wouldn't be organized in any kind of logical structure that would relate the things in the layout to the logical layout dictated by the application. So instead, when you dynamically create views, stuff them somewhere like a list so you can iterate through it later.

Categories

Resources