Showing posts with label Android Custom List. Show all posts
Showing posts with label Android Custom List. Show all posts

Saturday, May 3, 2014

Create Multiple Dynamic views to make Like ListView

Hi,
        Here is a list of  dynamic views. Most of the times we needs such type of designs which are unable to manage with default widgets. So I am creating here a dynamic view witch will shows in scroll view.

 This can be an alternative of custom ListView.

1. Write  this code to your activity_main.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:layout_margin="20dp"
    >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       
         >

        <LinearLayout
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</RelativeLayout>




2. Code for item.xml :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:background="#000000" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#000000">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="2dp"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

                <EditText
                    android:id="@+id/med_name"
                    android:layout_width="150dp"
                    android:layout_height="35dp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:background="@drawable/edittext"
                    android:singleLine="true" >

                    <requestFocus />
                </EditText>

                <RadioButton
                    android:id="@+id/radioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="25dp"
                     android:clickable="false"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                   android:clickable="false"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                      android:clickable="false"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/delete"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:text="Delete" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

 3.  Now write this code to MainActivity.java :

package com.example.dinamiclist;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    String[] name_list={"A","B","C","D","E","F","G","H"};
    String[] morn_list={"1","0","0","1","0","1","1","1"};
    String[] noon_list={"0","0","1","1","0","0","0","1"};
    String[] night_list={"0","1","1","0","0","1","1","0"};
    LinearLayout list;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(LinearLayout)findViewById(R.id.list);
        dynaicView();
       
    }

    private void dynaicView() {
        // TODO Auto-generated method stub
       
        LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < name_list.length; i++) {
            final View customView = linflater.inflate(R.layout.item,
                    null);
            final EditText name = (EditText) customView
                    .findViewById(R.id.med_name);
            final RadioButton rad1 = (RadioButton) customView
                    .findViewById(R.id.radioButton1);
            final RadioButton rad2 = (RadioButton) customView
                    .findViewById(R.id.radioButton2);
            final RadioButton rad3 = (RadioButton) customView
                    .findViewById(R.id.radioButton3);
            final Button delete = (Button) customView
                    .findViewById(R.id.delete);

            name.setId(i);
            rad1.setId(i);
            rad2.setId(i);
            rad3.setId(i);
            delete.setId(i);
            name.setText(name_list[i]);
            if (morn_list[i].equals("1")) {
                rad1.setChecked(true);
            }
            if (noon_list[i].equals("1")) {
                rad2.setChecked(true);
            }
            if (night_list[i].equals("1")) {
                rad3.setChecked(true);
            }

            delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    list.removeView(customView);
                   
                   
                }
            });

            list.addView(customView);
        }
    }
    

}





The out put will we like that:
Note:  No any change is there in manifest.xml

Hope this help you a lot......... Happy coding.