Layout Inflater

Web Hosting
In this tutorial, I will show you about Layout Inflater in android. See the out put below:

Output of layout_inflater.xml:


After you click the Add View Button. Here's the output below:


The content of content.xml display and when you click the Remove button, the content.xml form will be removed.


See the complete code below:

layout_inflater.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LayoutInflater" >

    <Button
        android:id="@+id/btn_addview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:text="Add View" />

    <Button
        android:id="@+id/btn_removeview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="19dp"
        android:layout_toRightOf="@+id/btn_addview"
        android:text="Remove" />

    <RelativeLayout
        android:id="@+id/rl_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_addview">
    </RelativeLayout>

</RelativeLayout>

content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="Fill Up The Form Below" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/et_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Address"
        android:ems="10" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

LayoutInflater.java

package com.client.app.views;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class LayoutInflater extends Activity implements OnClickListener{

RelativeLayout rl_content;
Button addview, removeview, btn_submit;
EditText et_name, et_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_inflater);
initializeVars();
}
public void initializeVars(){
rl_content = (RelativeLayout)findViewById(R.id.rl_display);
addview = (Button)findViewById(R.id.btn_addview);
removeview = (Button)findViewById(R.id.btn_removeview);
addview.setOnClickListener(this);
removeview.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.layout_inflater, menu);
return true;
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.btn_addview:
View view = getLayoutInflater().inflate(R.layout.content, rl_content,false);
rl_content.addView(view);
et_name = (EditText)findViewById(R.id.et_name);
et_address = (EditText)findViewById(R.id.et_address);
btn_submit = (Button)findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), et_name.getText().toString()+" "+et_address.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_removeview:
View myView = findViewById(R.id.ll_content);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
break;
}
}

}


AndroidManifies.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.client.app.views"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.client.app.views.LayoutInflater"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Web Hosting
That's it, Enjoy Coding. Hope this help you!