Saturday, February 15, 2014

What is LayoutInflater? Why we need to inflate and when?

LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in java programs.
In simple terms there are two ways to create UI in android. One is static way and another is dynamic or programmatically.
Suppose we have a simple layout main.xml  having one textview and one edittext as follow.

<?xml version="1.0" encoding="utf-8"?>
<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/layout1"
    >
<TextView
        android:id="@+id/namelabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter your name"
        android:textAppearance="?android:attr/textAppearanceLarge" >
    </TextView>
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="14dp"
        android:ems="10">
    </EditText>
</LinearLayout>
We can display this layout in static way by
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
Dynamic way of creating a view means the view is not mentioned in our main.xml but we want to show with this in run time. For example We have another xml in layout folder as  footer.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="Add your record"
    android:textSize="24sp" >
 </TextView>
We want to show this textbox in run time within our main UI. So here we will inflate text.xml . See how

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        TextView t = (TextView)inflater.inflate(R.layout.footer,null);

         lLayout = (LinearLayout)findViewById(R.id.layout1);

         lLayout.addView(t);

Here I have used getSystemService (String) to retrieve a  LayoutInflater instance. I can use getLayoutInflator() too to inflate instead of using getSystemService (String)  like below

 

LayoutInflator inflater = getLayoutInflater();

              TextView t = (TextView) inflater.inflate(R.layout.footer, null);

                               lLayout.addView(t);

We have inflated the view footer using inflater and converted it Textview object. In this example we have inflated a simple textview. If you want to know how to inflate a listview , check this Inflating Listview post.


Before Inflation
 
After Inflation
 

  You can download the complete source code from here.
You might be interested to know

How to inflate a listview?

26 comments:

  1. Thanks.. Your code helped to understand better..
    keepup the good work..

    ReplyDelete
  2. Thank you for your interest in helping others.
    Superb!!! Thanks for your code.

    ReplyDelete
  3. Nice Tutorial. Easy to understand.

    ReplyDelete
  4. Thank a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    full stack developer training in pune


    full stack developer training in annanagar

    full stack developer training in tambaram

    ReplyDelete
  5. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
    Click here:
    python training in chennai
    Click here:
    python training in chennai

    ReplyDelete
  6. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
    java course in tambaram | java course in velachery

    java course in omr | oracle course in chennai

    ReplyDelete
  7. I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!

    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  8. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Python training in marathahalli | Python training institute in pune | Python training in IndiraNagar

    ReplyDelete
  9. Great so we can inflate layout every activity where we want

    ReplyDelete
  10. This is a terrific article, and that I would really like additional info if you have got any. I’m fascinated with this subject and your post has been one among the simplest I actually have read.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  11. Thank You! For sharing such a great article, It's been a amazing article.
    It's provide lot's of information, I really enjoyed to read this,
    i hope, i will get these kinds of information on a regular basis from your side.

    Keyword: Best Plumbing Service
    Visit: Cheap hotel room
    Read More: Buy New Home

    ReplyDelete
  12. Thank You! For sharing such a great article, It's been a amazing article.
    It's provide lot's of information, I really enjoyed to read this.
    I hope, i will get these kinds of information on a regular basis from your side.
    I follows your site, and share your's content because it is so much useful.

    Source: Play Sports Free
    Keyword: Way to Earn Money
    Read: Wrestle Mania 36
    Watch: Wrestling Video

    ReplyDelete
  13. Very well explained, specially it is easy to understand for beginners

    ReplyDelete
  14. Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.I have recently visited your blog profile. I am totally impressed by your blogging skills and knowledge.Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article

    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete