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.
Related
Android's leanback library provides a few standard ways to customize a RowFragment, both the Rows and the Cards themselves, but I'm not sure how to add custom elements outside of what is provided on the framework.
What I am specifically trying to achieve is to add a FooterItem to a row, similar to how a each Row has a a HeaderItem.
I'd like to get the existing functionality in ListRowView / ListRowPresenter, but I'm not sure how to do it "properly".
As with almost all things with Leanback, there's more than one way to skin the cat.
We needed to customize the ListRow to add more padding to the header. We've achieved this level of customization by overriding onBindRowViewHolder of ListRowPresenter, grabbing the View for the header (via View header = holder.getHeaderViewHolder().view;). And then we changed the height of it with header.getLayoutParams().height = rowItem.getRowHeaderHeight();.
With this same technique, you could dynamically add your footer view to holder.view (with addView).
Another approach would be following the what the documentation for RowPresenter recommends. The relevant quote is:
When a subclass of RowPresenter adds UI widgets, it should subclass RowPresenter.ViewHolder and override createRowViewHolder(ViewGroup) and initializeRowViewHolder(ViewHolder). The subclass must use layout id "row_content" for the widget that will be aligned to the title of any HeadersFragment that may exist in the parent fragment.
So with this new recommended approach, you'll basically be supplying your own xml. I believe you can even subclass ListRowPresenter. To see how the code works, look at ListRowView to see that it inflates the xml for R.layout.lb_list_row. And then it just uses the view for R.id.row_content.
Then that'll be the actual view that's supplied by holder.view and then you can set text on it and do whatever you want.
Let me know if you need any further clarification.
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
Difficult to try and phrase this question, but I will try my best.
Basically, I have an application and I would like to split the code up more. To try and explain this, will give an example of one of my screens.
In my home screen, I have a title, user details, balance, next bill details and usage details. I want to split each of these sections into their own views. So what I have in my main XML file is I have 5 different RelativeLayouts, like this
<RelativeLayout android:orientation="vertical"
android:layout_below="#+id/title" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/layout1">
</RelativeLayout>
One for each function I want to have on my home screen. And as you can see I also have each layout set below the previous layout, so the order is title, layout1, layout2, layout3, layout4. (The reason I am doing this is, that I want each layout to be interchangable, i.e. I could remove layout2, and order then be title, layout1,layout3,layout4 as I may not need the 2nd layout depending on what is required of the app)
So in my main activity class, I have called each of these layouts.
layoutTitle = (RelativeLayout) findViewById(R.id.title);
layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout2 = (RelativeLayout) findViewById(R.id.layout2);
layout3 = (RelativeLayout) findViewById(R.id.layout3);
layout4 = (RelativeLayout) findViewById(R.id.layout4);
Then I use layout inflator like so
View view;
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.userdata, null);
to set up the layout for each layout I have defined in my xml file.
What I want to know is, is it possible to inflate another activity rather than an XML file? The reason I ask this is, that each function I said the home screen is made up of, title, user details, balance etc, I have an activity for each of these. Each of these functions has custom functionality that I have designed, such as animation etc. If I simply inflate these xml files, then I can't access the widgets inside so I can't set up the functionality from Java which is what I need.
If I was able to inflate another activity, then this would not be a problem, as I would set up the functionality for each function in activity, then just inflate that class.
I hope my question was clear, if not I can expand on any areas you are not sure about.
Would be very grateful for any assistance!!
EDIT: If it is not possible to inflate a class, then is there any other way method you could recommend that could solve my problem?
I do exactly the same as you.
I use composition in my Java activities to achieve the sharing of functionalities across my activities. I think of activities as "context", and not as parts of my page that get replicated. So when I am in a certain context (an activity), I then display layout1, layout2 and layout3 with specific parameters, and specific contents that depends on that activity. On another activity, layout1, 2 or 3 could be different, but they have the same location on my screen all the time.
I use composition through views: all my activities have a superclass (call it anything you want, something like ActivityWithCustomLayout, which contains all my layout as class members (as ViewGroup). They are protected, so each of the variables layout1, layout2 and layout3 are available to all subclass activities of this superclass.
And when on a specific activity, I populate each of the layouts on my onCreate method with:
layout1.addView(...something inflated from an XML that depends on that specific activity...);
layout2.addView(...same principle...);
So in fact all my XML layouts are "parts" of activities, which I inflate at runtime into views, that I add dynamically to my activity when needed.
You're right, these explanations are not easy :)
If you target Android >= Honeycomb (including ICS) then have a look at the Fragment framework, it may be a simpler way to achieve all of that (haven't had a look at that yet).
Not too much of an answer but I often find myself subclassing the layouts themselves. Then, in the onFinishInlate() callback, I wire up all of my view references using findByViewId. In your XML, you can replace <RelativeLayout> with <com.company.CustomLayout>, assuming that your CustomLayout is a subclass of RelativeLayout.
As mentioned, you can then pull them out into separate files and include them with the include tag. This makes refactoring easy and allows you to reuse the layout components.
Or, if you inflate these subclassed layouts from within your code, you don't have to worry about all the messy findViewById calls in your activity.
Fragments are also great and have similar life cycles to an activity. Good luck!
I'm not sure that you need to inflate all of those Views from within your Activity. Look into using the include and merge tags within your Layout. This should help get you started: http://developer.android.com/resources/articles/layout-tricks-merge.html
You can also toggle the visibility of your Layouts. So, you can declare them in XML, initialize them as visibility="false", and then toggle that visibility in your Java code.
You also stated: "If I simply inflate these xml files, then I can't access the widgets inside so I can't set up the functionality from Java which is what I need."
You can access any Layout component after it has been added to your Activity using findViewById and casting the object it returns to the appropriate type.
i have a complex view which contains 4 list views arranged as per the requirement. i have been been able to implement and get it working. but this is a sole activity and data needs to supplied internally (within the activity).
i want to define a way where in, this complex view is like a reusable component which is called by other activities that provide data for all 4 list views and then the view shows up in the screen.
could somebody please guide me as to how do i go about achieving this functionality.
You should define your listview structure in a layout file of its own. Then you can use whats called inflation, which lets you "inject" seperate layout files into your main layout in run-time. Take a look at:
http://developer.android.com/reference/android/view/LayoutInflater.html
Take note at the introductory notes. Android is already inflating an XML resource, namely the layout file you´ve defined in setContentView(), you can grab the current instance of the inflater and use it as you see fit, saving greatly on memory as opposed to instantiating it yourself.
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);