I am unable to understand the exact use of LayoutInflater in Android.
What exactly the role of LayoutInflater is ? I need someone to explain this to me.
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>
You give it to
View v = li.inflate(R.layout.my_layout,null,false);
What did Layout Inflater do ?
v is now a
LinearLayout object (
View) , and contains a
TextView object, arranged in exact order and with all properties set, as we described in the XML above.
LayoutInflater reads an XML in which we describe how we want a UI layout to be. It then creates actual
Viewobjects for UI from that XML.