Home » Android » Inflator in Android Application Development

Inflator in Android Application Development

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&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.

Answer:

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.

Answer:

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.

Answer:

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