Layout Inflator in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Layout Inflator in Android

Postby vivekkalady » Mon Nov 24, 2014 1:49 pm

Layout-Inflater class is used to instantiate layout XML file into its corresponding View objects.In other words, it takes as input an XML file and builds the View objects from it.

Let’s first take a look at how LayoutInflater works. There are two usable versions of the inflate() method for a standard application:

inflate(int resource, ViewGroup root)
inflate(int resource, ViewGroup root, boolean attachToRoot)


The first parameter points to the layout resource you want to inflate. The second parameter is the root view of the hierarchy you are inflating the resource to attach to. When the third parameter is present, it governs whether or not the inflated view is attached to the supplied root after inflation.

Will make a simple application for Android to understand about layout inflater.

Create a project and go to the main xml file which looks like this

Code: Select all

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_layout_id">
</LinearLayout>


create another xml file in layout folder and name it layout_item.xml

Code: Select all

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layout_item_id">
 
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>
 
</LinearLayout>



MainActivity.java will look like this

Code: Select all

 
public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        //here we set the main layout
        // this is the parrent layout...here we will add the view we created
        setContentView(R.layout.main);
 
        //call the main layout from xml
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
 
        //create a view to inflate the layout_item (the xml with the textView created before)
        View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
 
        //add the view to the main layout
        mainLayout.addView(view);
    }
}



What will happen is that the
getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
will return a view
that view is added to the mainLayout. so will see the content of layout_item.xml will be added as a view to screen.


Adapters are the most common case for using LayoutInflater is custom ListView adapters overriding getView(), which has the following method signature:

Code: Select all

getView(int position, View convertView, ViewGroup parent)


Code: Select all

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflate(R.layout.item_row, parent, false);
    }
 
    return convertView;
}



Fragments also use inflation often when creating views via onCreateView(); notice its method signature:

Code: Select all

onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)


Code: Select all

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron