Thursday, 22 May 2014

MultiSelection Mode Enabled In ListView By Adding Toggle Button Using Custom Layout In Android

MultiSelection Mode Enabled In ListView By Adding Toggle Button

1)Create a new android application project in eclipse 

2))Then after craeting a new project you have to edit the activity_mail.xml file , As shown below.

3)Create a layout file for customizing list view items in the file 

4) Now you have to code the main activity  file as shown below
                                                                      public class MainActivity extends Activity {
    String[] countries = new String[] {
        "Sri Lanka",
        "North Korea",
        "South Korea",
    // Array of booleans to store toggle button status
    public boolean[] status = {
    public void onCreate(Bundle savedInstanceState) {
        /** Restore from the previous state if exists */
            status = savedInstanceState.getBooleanArray("status");
        ListView lvCountries = (ListView) findViewById(;
        OnItemClickListener itemClickListener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> lv, View item, int position, long id) {
                ListView lView = (ListView) lv;
                SimpleAdapter adapter = (SimpleAdapter) lView.getAdapter();
                HashMap<String,Object> hm = (HashMap) adapter.getItem(position);
                /** The clicked Item in the ListView */
                RelativeLayout rLayout = (RelativeLayout) item;
                /** Getting the toggle button corresponding to the clicked item */
                ToggleButton tgl = (ToggleButton) rLayout.getChildAt(1);
                String strStatus = "";
                    strStatus = "Off";
                    strStatus = "On";
                Toast.makeText(getBaseContext(), (String) hm.get("txt") + " : " + strStatus, Toast.LENGTH_SHORT).show();
        // Each row in the list stores country name and its status
        List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<10;i++){
            HashMap<String, Object> hm = new HashMap<String,Object>();
            hm.put("txt", countries[i]);
        // Keys used in Hashmap
        String[] from = {"txt","stat" };
        // Ids of views in listview_layout
        int[] to = {,};
        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.lv_layout, from, to);
    /** Saving the current state of the activity
    * for configuration changes [ Portrait <=> Landscape ]
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBooleanArray("status", status);
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

5)Now the listview with toggle button  is ready             

  Download Sourcecode


Post a Comment

Total Pageviews

Contact Form


Email *

Message *