Friday 28 September 2012

ExapndableListView With Checkbox Sample

ExpandableListView

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view. 

 Source Code 

main.xml

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

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>


group.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="40dip"
        android:text="Items"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


child.xml 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textViewchild"
        android:layout_width="220dp"
        android:layout_height="40dip"
        android:text="child values "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/checkBoxchild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

above xml files are declared for Main page where you can get the component Representation 

and the other two are used to get the parent and child values 

Activities of which uses the above xml files are shown below 

package com.pack.android;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class CoutmLIstActivity extends ExpandableListActivity {

    DisplayMetrics dismat;
    ExpandableListView matrix;
    int width;
    static final String Groupvalues[] = { "Android", "Flex", "Voice Xml",
            };
    static final String childvalues[][] = {
            { "SaiNath", "Anudeep", "Deppeak"},
            { "Kranthi", "Ramakrishna", "Nagarjuna" }, { "Sushma", "Meghana", "Sathya" } };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExpandableListView ep = getExpandableListView();
        dismat = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dismat);
        width = dismat.widthPixels;
            ep.setIndicatorBounds(width - GetDipsFromPixel(50), width
                - GetDipsFromPixel(10));
        ep.setAdapter(new ExpandbleAdpter(CoutmLIstActivity.this));

        ep.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
               
            }
        });
    }

    public int GetDipsFromPixel(float pixels) {
        final float scale = getResources().getDisplayMetrics().density;
                return (int) (pixels * scale + 0.5f);
    }

}

Adapter class for List Component 

package com.pack.android;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ExpandbleAdpter extends BaseExpandableListAdapter {

    Context myconContext;

    public ExpandbleAdpter(Context context) {
        myconContext = context;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater lay = (LayoutInflater) myconContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = lay.inflate(R.layout.child, null);
        }
        final TextView name = (TextView) convertView
                .findViewById(R.id.textViewchild);
        name
                .setText(CoutmLIstActivity.childvalues[groupPosition][childPosition]);

        CheckBox child = (CheckBox) convertView
                .findViewById(R.id.checkBoxchild);
        child.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                Toast.makeText(myconContext,
                        "Name" + name.getText().toString(), Toast.LENGTH_LONG)
                        .show();
            }
        });

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return CoutmLIstActivity.childvalues.length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return CoutmLIstActivity.Groupvalues.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater lay2 = (LayoutInflater) myconContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = lay2.inflate(R.layout.group, null);

        }
        TextView cidlvalue = (TextView) convertView
                .findViewById(R.id.textView1);
        cidlvalue.setText(CoutmLIstActivity.Groupvalues[groupPosition]);

        return convertView;

    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}

Finally the tree view looks like as fallows