I am unable to understand the use of LayoutInflater in Android.
What exactly is the role of LayoutInflater, and how to use it for a simple Android app?
I am unable to understand the use of LayoutInflater in Android.
What exactly is the role of LayoutInflater, and how to use it for a simple Android app?
What is Layoutinflater ?
LayoutInflater
is a class (wrapper of some implementation or service), you can get one:
LayoutInflater li = LayoutInflater.from(context);
How to use Layoutinflater ?
You feed it an XML layout file. You need not give full file address, just its resource id, generated for you automatically in R
class. For example, a layout file which look like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
saved as /res/layout/my_layout.xml
.
You give it to LayoutInflater
like:
View v = li.inflate(R.layout.my_layout,null,false);
What did Layout Inflater do ?
That v
is now a LinearLayout
object (LinearLayout
extends View
) , and contains a TextView
object, arranged in exact order and with all properties set, as we described in the XML above.
TL;DR: A LayoutInflater
reads an XML in which we describe how we want a UI layout to be. It then creates actual View
objects for UI from that XML.
when you run setContentView(layout file name)
, you can run findViewById(id of the widget)
. You dont need to do something like xyz.findViewById
. The context of your app is set to that layout file and all findBiewById
call will refer to that layout file.
There are cases when you need to pick up one more layout file, like a CustomDialog, ListElement or a Custom Toast. At this time you wont want to create a Activity just for these small UI components, that that time you programmatically need to get a programmatic reference to your layout file, so that you can run findViewById on it.
Inflate, blows the layout like a balloon and give you the balloon for you to watch around it all the colors and objects drawn on it :). Inflate gives you the object reference to that layout to call findViewById on.
Hope this clears.
Some basics for LayoutInflater-
This class is used to instantiate layout XML file into its corresponding View objects.
It is never used directly. Instead,
getLayoutInflater()
or getSystemService(String)
to retrieve a standard LayoutInflater instance that is already hooked up to the current context.From the Android docs:
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
Useful in creating Custom Views
It is most likely to get closed but the role of layout inflater is to create a view
from the supplied layout file
View row=getLayoutInflater.inflate(R.layout.list_item,parent,false);
it is creating the view from the R.layout.list_item
file and proving that view
.