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);
Related
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 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
The app I am working on requires a dynamic layout that displays a HorizontalScrollView of profiles. A profile is simply a RelativeLayout with a picture and some text. Since I get the data from a data file, I need to create a Relative layout for each of profiles. At first I created each RelativeLayout programmatically in a for loop and then added it to the parent view. This works, but I don't want to do it this way. I want to use different layout files based on the screen size of the device, etc.
Then I thought. Well, what if I had a Layout with just one profile on it? My code could get that one profile with findViewById() and then create new ones based off of it! In other words:
// get the layout
profileLayout = (LinearLayout) findViewById(R.id.profileLayout);
// get the first profile in the layout
originalProfile = (RelativeLayout) findViewById(R.id.profile1);
// make copy of profile
temporaryProfile = originalProfile;
// make changes to the this profile and add it back
profileLayout.addView(temporaryProfile);
Of course, this doesn't work because this is java and temporaryProfile is now a reference to originalProfile. So is there any way to make a copy of this RelativeLayout? I'm aware of LayoutInflater, but I still don't understand how it works. There is also Object.clone().
Not so much. Sounds like your particular case could benefit from a ListView of some kind and an adapter (see resources like this for info). If not, then a LayoutInflater is the best answer as it does precisely what you want. Acquiring one you can then use it to "inflate" any view you've defined in your XML layout files and do whatever you want with it.
Here's a great discussion of the inflater.
you can make a copy of your inflator with a new context. Layout Inflater - Android Developers
Why not simply use an include in the xml file for the layout that you want.
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);
I'm reading the book 'Hello, Android'. In the Sudoku example, it uses a options menu. It needs a MenuInflater that we use to read the menu definition from XML and turns it into a real view.
To use button, textview, or many other views, I don't need to inflate them.
My question is, in what situations, I need inflaters? Why doesn't Android treat menus like other views?
You need an inflater at every place that you want to dynamically create a view out of an XML file.
Activity layouts are automatically inflated when you call setContentView() as they're always required.
But when the menu is required — which is only when the user first presses the Menu button — the XML-defined layout needs to be manually inflated.
Similarly, if you have a ListView, you don't know in advance what rows will exist, so we have to inflate a View from XML for each row in the list, as they're required.
Inflaters are mainly used for parsing Xml layout into view objects. As mentioned above the inflation is needed for making a link between the UI defined in the Xml for manipulating and making developer.
Whenever UI Updation is needed we need inflation and UI Updation is done through view object and developer can dynamically create view and add to existing view.
Hence inflation helps developer to change behaviour of UI in xml layout according to specified condition in a program.
With inflation, we are able define controllers in MVC for each xml layout where xml is view.
Menu is also a view it has to inflated In certain code such setContentView(specifiedLayout) includes inflation
But in earlier version it was not like this it was like setContextView(getInflater().inflate(specifiedLayout))
for ease of programming,android developers have incorporated inflation in setContentview() and there are lot scenarios like add view to layout addView(),etc..In most cases inflation has incorporated in code that why most of beginner does know inflation concept and has difficulties in understanding inflation in android.