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.
Related
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.
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 am new to android development and I will be developing one android application. In this app I want to generate xml layout dynamically. After login to the app, server will send xml template with description of controls including different attribute description(validations on each control,maxlength,size of the control etc.), which means there will be no predefined xml layout.
My plan is, from downloaded xml template I will create a xml layout and store that xml in sqlite database. When user clicks on a record I want show this dynamically created form to the user.
Is it possible to modify xml layout(like setting innerxml)?
You could try something like that :
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(myXmlPullParser, null);
setContentView(myView);
Based on View documentation:
Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
You can't inflate layout if it is not inside layout res directory.
it is possible.take one xml layout with only linear layout and in the onCreate access the layout with id and create all the views programatically and add to the linear layout as subviews and later set that xml layout as setContentView.
later you want to add more view after View Created you can run UI thread and add the views.
I think rather than creating xml dynamically you should be looking at creating the views you want programatically ,
If its an advanced view you should do as TheWAlkingCube suggested, inflate a xml view dynamically.
Android does not support your way of working entirely, but you can build up your own layout dynamically.
Create your own TemplateBuilder-class. This class will retrieve a layout file from your server (and possible save it in your database). The XML file can be read out and you can add each defined item at runtime. As said before, this is not the conventional way you should work in Android, but it will be your best option.
If you have certain ViewGroups or layouts which will be used in the layouts more often, you can define them as XML files in your res-folder and inflate them when needed. Creating predefined layout-blocks you can inflate whenever you want and literally build up your custom layout.
For an example for adding view at runtime you can visit the link below:
http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/
if you insist on doing it this way, you have to parse the xml file received, in your program and add each view of the xml layout programmatically based on how it is requested in the file received
I am still fairly new to android, but I am getting there!
I have created an ExpandableListView almost identical to the api sample code on the android developers website. The ExpandableList works wonderfully.
I tried creating custom layouts through xml and a SimpleExpandableAdaptor and had nightmares. Which brings me to the point:
I have been reading a lot about LayoutInflaters and how well they work with XML files. However, I would like to create two seperate ImageButtons and place them next to the groupExpanded indicator (in the group TextView) with out referencing xml. I don't think I can use xml to do this simply because the code supplied by API doesn't rely on xml, its created runtime.
Can LayoutInflater work in this situation (no xml)? Does anyone have suggestions where I might look for a clean solution?
Thanks in advance.
You can do both, either inflate a xml layout using a LayoutInflater or creating the View elements using the Android object model. I would suggest using xml files as you can easily change the layout to a later point without touching the code.
Have a look at this blog post it shows how to inflate layouts in an adapter for a ExpandableListView.
I believe LayoutInflater is used for the express purpose of loading an XML layout into a View object. Alternately, you can create your own view by constructing RelativeLayout/LinearLayout etc. and programmatically adding your ImageButtons to it
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(imageButton1);
layout.addView(imageButton2);
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);