Why to use Inflater in listview - android

I always had ambiguity on why we need to use inflater in android, Why are they used in ListView for custom layouts (like below)?
What is an Inflater ?
What is the advantage of using Inflater ?
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
Thanks,

What is an Inflater ?
To summarize what the LayoutInflater Documentation says... A LayoutInflater is one of the Android System Services that is responsible for taking your XML files that define a layout, and converting them into View objects. The OS then uses these view objects to draw the screen.
I always had ambiguity on why we need to use inflater in android, Why
are they used in android ListView for a custom layout ?
Typically, you don't ever need to directly use a LayoutInflater. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. So you, as the programmer, are responsible for making sure the views are inflated. Now you want to inflate views in the context of a ListView. The Adapter class can do the inflation for you if you do not want to customize each item. But if you want to customize the views shown in a list, you will have to manually inflate each view with the LayoutInflater, since there is no other existing method you can use.
What is the advantage of using Inflater ?
There is no advantage to using it. You are required to use a LayoutInflater in some shape or form to inflate your static XML layouts.
Alternatively, you could create views dynamically with java code. However, you would need to call methods to set each property for the view by hand. In my opinion, it is easier to use the XML/inflation process. In addition, Android pre-processes your XML files at build time, so this results in a faster execution time.

I always had ambiguity on why wee need to use inflater in android, Why are they used in android ListView for a custom layout ?
They are used to create the Views for each row.
What is an Inflater ?
A system service that creates a View out of an XML layout.
In the below code i am trying to understand why Inflator is used ?
The inflater is used to create the Views for the rows.
What is the advantage of using Inflater ?
Compared to what? How do you want to create the Views out of the XML layout?

Put simply, an inflater allows you to create a View from a resource layout file so that you do not need to create everything programmatically.
In your example, you inflate the layout R.layout.list_mobile. This lets you access all of the views within it. For example, you then call:
TextView textView = (TextView) rowView.findViewById(R.id.label);
By calling rowView.findViewById() you are able to access views that were created within that layout. Often for ListViews you will have a row XML file that you then inflate and put your data into the views.

Related

What is the LayoutInflater in Android? How does it differs from findViewById?

An example LayoutInflater code:
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.,parent,false);
Can't understand the work of LayoutInflater in Android. Tried to read the official documentation of the android but, couldn't get the concept.
The main difference between findViewById and LayoutInflater is that findViewById is used to access existing views in a layout, while LayoutInflater is used to create a new view hierarchy from an XML layout file.
FindViewById is a method of the Activity class, while LayoutInflater is a separate class on it's own.
FindViewById is used to access a view in the current layout, this means lets us find Views from layouts written in XML and returns a reference to their Java objects, while LayoutInflater is used to create a new view hierarchy from an XML layout file, so this class is the responsible for “inflating” the layouts.
Hope it helps.

Create a dynamic Custom View in Android

I need to generate a dynamic custom view in my application.
I need a custom view that is formed by an image button and 2 text views, above and bellow the button. There should also be an onClick listener on the image button, that will call a function when pressed.
By dynamic I mean those views will be created on demand, there should be a "dummy" structure from which I should be able to create as many custom views as I need.
How can I do that?
Just create the layout for your view with an xml and then use the layout inflater:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View yourView = inflater.inflate(R.layout.view_layout, parent, false);
To access it's components then you use:
ImageButton imageButton = (ImageButton) yourView.findViewById(R.id.button);
You can start by creating a custom view class MyCustomView that extends the View class. Create a separate XML layout (the dummy structure) for the custom view and inflate it into the custom view constructor,
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_custom_view_layout, this, true);
You can add any listeners to the Button views that will make part of this compound view class. Take a look at this link for further understanding: http://www.vogella.com/tutorials/AndroidCustomViews/article.html
At the end, just treat this object as just a view and you place it on the interface wherever you want.

Add XML content to a View

im developing an application and I need to add elements dynamically. I wonder if I can append elements (stored in a XML file) in my current Activity, like innerHTML in JavaScript.
I tried LayoutInflater but that replaces all the content and I need to append.
Thanks!
The easiest way to do this is to use the LayoutInflater as you said. I'm not sure how you were doing it (hence why I asked to see your inflating code), but the simplest way to understand is to do the following:
LayoutInflater inflater = getLayoutInflater();
View viewToAppend = inflater.inflate(R.layout.some_layout, null);
// Optional, create LayoutParams and apply to view with
// viewToAppend.setLayoutParams(params);
mainView.addView(viewToAppend);

Inflator in Android Application Development

Can anybody please tell What Inflator is and how it is being used in an Android application?
I don't know the exact use of it and Why it is being used.
My preferred way to handle inflation:
//First get our inflater ready, you'll need the application/activity context for this
LayoutInflater mInflater;
mInflater = LayoutInflater.from(mContext);
//Inflate the view from xml
View newView = mInflater.inflate(R.layout.my_new_layout, null);
//Then you'll want to add it to an existing layout object
mMainLayout.add(newView);
//Or perhaps just set it as the main view (though this method can also
// inflate the XML for you if you give it the resource id directly)
setContentView(newView);
Basically, you use it to inflate existing xml layouts at runtime. Usually you go ahead and insert those new views into previously defined ViewGroups or List objects.
Not quite sure what you mean, but if its related with inflating views, its used to load layout xml files into your application. e.g by
View myWelcome = View.inflate(this, R.layout.welcome, null);
Its easier and consider best practice to have you view definition inside layout xml files, instead of creating your views fully by code.
layout inflator is used to return a java object of your complete layout
suppose you have a layout xml file in which the root element is relative layout and it contains a imageview and textview then using layout inflator you can return a view object that refers to entire layout.
this basically is used in list view and grid view to plug into them a layout object of single row or element which is to be repeated.
you were asking for use of Inflator..
basically when you want to use two xml files in one java class ,inflator is used and its code is simple which is given below..
TextView text;
View layout;
LayoutInflater inflator=getLayoutInflater();
layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null);
text=(TextView)layout.findViewById(R.id.text);
text.setText("progressing");
here i use textview,this is present in next xml with id=text
thats it..
if you find this worthy then please like this..
thanks

How to add layout from xml to Extended FrameLayout Android?

I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to the NoteView.
The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.
How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?
Try LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myView = (LinearLayout) inflater.inflate(R.layout.my_layout, null);
frameLayout.addChild(myView);
Have a look at the documentation for LayoutInflater here: http://d.android.com/reference/android/view/LayoutInflater.html

Categories

Resources