Showing posts with label adding UI components dynamically. Show all posts
Showing posts with label adding UI components dynamically. Show all posts

Wednesday, 13 March 2013

Inflating UI Dynammically Example in Android

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="29dp"
            android:text="Add" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="36dp"
                        android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/add_button"
            android:text="Delete" />

        <LinearLayout android:id="@+id/child_linear"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/add_button"
            android:layout_marginTop="45dp"
            android:orientation="vertical" >
        </LinearLayout>

    </RelativeLayout>

custom_layout.xml


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

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

</LinearLayout>

MainActivity.java

package com.rajeshvijayakumar.inflat;

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

public class MainActivity extends Activity implements OnClickListener {

    private Button mAddButton;
    private Button mDeleteButton;
    private LinearLayout mLinear;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAddButton = (Button) findViewById(R.id.add_button);
        mDeleteButton = (Button) findViewById(R.id.delete_button);
        mLinear = (LinearLayout) findViewById(R.id.child_linear);
        mAddButton.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);
       
    }

    @Override
    public void onClick(View v) {
       
        switch(v.getId()) {
            case R.id.add_button :
                View childView = getLayoutInflater().inflate(R.layout.custom_layout, null);
                mLinear.addView(childView);
                break;
            case R.id.delete_button :
                int childSize = mLinear.getChildCount();
                if(childSize != 0) {
                    mLinear.removeViewAt(childSize - 1);
                }
                break;
        }
    }  
}

Output :






Source Code :  Download this Example Here