Android LayoutInflater Turorial

Android LayoutInflater Turorial

In this tutorial I will show you how to use the LayoutInflater in Android.1. Make a new project (I called my project InflaterExample and my main Activity it’s called “MyActivity”)
2. Go to res – layout – main.xml and put an id called “main_layout_id”

<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>

3. Go to res – layout and make a new xml called “layout_item.xml” . Now you must have 2 xml files: main.xml and layout_item.xml
4. We will make a TextView in the xml created before. This will be the view that we want to inflate (add) to the main.xml . Put the code below in the layout_item.xml

<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>

5.Now in your main activity you will have to call the main.xml with findViewById because it is the parent that will host our view.

//call the main layout from xml
 LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);

6. At this step we have to create a view to inflate the layout_item.xml.In the inflate method we have to say first WHAT we want to inflate and then WHERE we want to inflate our view. We use the “false” attribute because this way any further layout changes we make to the loaded view,will take effect. If we have set it to “true” it would return the root object again, which would prevent further layout changes to the loaded object (unless we can find it using findViewById). You can find more explanations here: stackoverflow

//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);

7. And the last step, add the view to the layout

//add the view to the main layout
mainLayout.addView(view);

8. Here is the whole MyActivity class:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

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);
    }
}

Now run the project. This should be the result of our tutorial 🙂

DOWNLOAD the project: LayoutInflater

,
Previous Post
Android simple splash screen
Next Post
How to Add AdMob to Android [Deprecated]

Related Posts

Menu