Wednesday 5 June 2013

Android Layout Inflator

Android Layout Inflator


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.
So we will make a simple application for Android to more understand about layout inflater.
Step 1: Make a New Project.
Step 2: Go to res/main.xml, provide some specific-id for linearlayout.
main.xml
 <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>
Step 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
Step 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
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>

Step 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.
 LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
Step 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).  
View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
Step 7: And the last step, add the view to the layout
mainLayout.addView(view);
MainActivity.java
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);
    }
}



4 comments:

  1. thank you so much this is very use full

    ReplyDelete
  2. very clearly explained...!!!

    ReplyDelete
  3. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your

    feed and I hope you post again soon.
    Mobile App Development Company
    Mobile App Development Company in India
    Mobile App Development Companies

    ReplyDelete