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
Related
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.
Thanks in advance...
I allow users to drag/drop views(text, image, etc) onto a RelativeLayout to create 'presentations'. I would ideally like to extract the resulting xml so the presentation can be saved to a fragment or .axml file, and recreated later either on the same device or a different one. I know I can iterate the children and manually create the xml I need, but it seems to me that since it must exist in the relative layout, that there is a way to extract it. Any ideas are appreciated!
You can't extract the XML for dynamically generated views. But what you can do is use XMLSerializer to make an XML of that view that you're generating. Check out my question.
To be frank, you can not inflate that XML dynamically (in future if you were going to do that). So, make sure you know what you're doing.
Is there any possible method to dump programmatically generated Layout?
For example I create Layout
LinearLayout mainLayout = new LinearLayout(this.mContext);
//...some code here
mainLayout.addView(picker);
mainLayout.addView(mOldColor);
mainLayout.addView(separator);
mainLayout.addView(mNewColor);
Now how to dump to Lod.d for example and get XML representation of Layout with attributes?
Afraid not. Views themselves know nothing about the xml they came from, so there's no way to force them to serialize back to xml.
Now if you wanted to do it on your own you can walk a view hierarchy fairly easily. But since views don't turn in to xml, you'd have to query each property individually and build the xml by hand. And it wouldn't work at all for custom views that could have custom attributes. And you'd have to use reflection to get the class name for the view type in the xml. Basically a lot of work for something fairly fragile.
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 want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);