Saturday 6 August 2011

Android expandable list view

Activity class..

public class ExpList extends ExpandableListActivity
{
    static final String colors[] = {
      "grey",
      "blue",
      "yellow",
      "red"
    };

    static final String shades[][] = {
// Shades of grey
      {
        "lightgrey","#D3D3D3",
        "dimgray","#696969",
        "sgi gray 92","#EAEAEA"
      },
// Shades of blue
      {
        "dodgerblue 2","#1C86EE",
        "steelblue 2","#5CACEE",
        "powderblue","#B0E0E6"
      },
// Shades of yellow
      {
        "yellow 1","#FFFF00",
        "gold 1","#FFD700",
        "darkgoldenrod 1","    #FFB90F"
      },
// Shades of red
      {
        "indianred 1","#FF6A6A",
        "firebrick 1","#FF3030",
        "maroon","#800000"
      }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        SimpleExpandableListAdapter expListAdapter =
            new SimpleExpandableListAdapter(
                this,
                createGroupList(),    // groupData describes the first-level entries
                R.layout.group_row,    // Layout for the first-level entries
                new String[] { "colorName" },    // Key in the groupData maps to display
                new int[] { R.id.groupname },        // Data under "colorName" key goes into this TextView
                createChildList(),    // childData describes second-level entries
                R.layout.child_row,    // Layout for second-level entries
                new String[] { "shadeName", "rgb" },    // Keys in childData maps to display
                new int[] { R.id.childname, R.id.rgb }    // Data under the keys above go into these TextViews
            );
        setListAdapter( expListAdapter );
    }

    private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < colors.length ; ++i ) {
        HashMap m = new HashMap();
        m.put( "colorName",colors[i] );
        result.add( m );
      }
      return (List)result;
    }

  private List createChildList() {
    ArrayList result = new ArrayList();
    for( int i = 0 ; i < shades.length ; ++i ) {
// Second-level lists
      ArrayList secList = new ArrayList();
      for( int n = 0 ; n < shades[i].length ; n += 2 ) {
        HashMap child = new HashMap();
        child.put( "shadeName", shades[i][n] );
        child.put( "rgb", shades[i][n+1] );
        secList.add( child );
      }
      result.add( secList );
    }
    return result;
  }

}

xml files

main.xml

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

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

     <TextView android:id="@+id/android:empty"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:text="@string/main_no_items"/>
</LinearLayout>

group_row.xml

<TextView android:id="@+id/groupname"
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:textSize="16px"
         android:textStyle="bold"
         android:paddingLeft="50px"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/red_button"/>



child_row.xml

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

    <TextView android:id="@+id/childname"
         android:paddingLeft="50px"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="150px"
         android:layout_height="wrap_content"/>

    <TextView android:id="@+id/rgb"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="100px"
         android:layout_height="wrap_content"/>
</LinearLayout>

In drawable folder...create one red_button.xml file

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <!--  <color android:color="#800517" />-->
        <shape>
            <gradient android:startColor="#FFFF00" android:endColor="#AF7817"
                android:angle="270" />
            <stroke android:width="5dp" android:color="#747170" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />

        </shape>
    </item>
    <item android:state_focused="true">
        <!--  <color android:color="#00FFFF" />-->
        <shape>
            <gradient android:endColor="#F433FF" android:startColor="#F6358A"
                android:angle="270" />
            <stroke android:width="5dp" android:color="#747170" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>

    </item>
    <item>
                <shape>
            <gradient android:endColor="#A0CFEC" android:startColor="#736AFF"
                android:angle="270" />
            <stroke android:width="5dp" android:color="#747170" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>

    </item>
</selector>


output screenshot...

No comments:

Post a Comment