Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflator in Android Application Development

Tags:

android

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.

like image 231
David Brown Avatar asked Oct 05 '10 06:10

David Brown


People also ask

What is an inflator in Android?

Inflating is the process of adding a view (. xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so: Button but = new Button(); but.

Why We Use inflate in Android?

As the title says, this post deals with LayoutInflater in Android and its most common usage with the inflate method. This method is used to create a view object from an XML file (where the UI designs are created). Then, you can have this view show up wherever you want (of course, in your app, not your freezer 😋).

What is inflate in Android Kotlin?

In this article, you will learn how to inflate one layout in another at Runtime using Kotlin in Android. To populate very few items, we can embed or inflate them on runtime. But for a long list, its always better to use RecyclerView. Following is the basic code to inflate one layout in another in Kotlin.

What is the purpose of a layout Inflater?

The LayoutInflater class is used to instantiate the contents of layout XML files into their corresponding View objects. In other words, it takes an XML file as input and builds the View objects from it.


3 Answers

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.

like image 98
Charles B Avatar answered Sep 30 '22 13:09

Charles B


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.

like image 33
Vidar Vestnes Avatar answered Sep 30 '22 15:09

Vidar Vestnes


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.

like image 41
Aakash Chotrani Avatar answered Sep 30 '22 14:09

Aakash Chotrani