MultiSelection Mode Enabled In ListView By Adding Toggle Button
Download Sourcecode
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[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; // Array of booleans to store toggle button status public boolean[] status = { true, false, false, false, false, false, false, false, false, false }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** Restore from the previous state if exists */ if(savedInstanceState!=null){ status = savedInstanceState.getBooleanArray("status"); } ListView lvCountries = (ListView) findViewById(R.id.lv_countries); OnItemClickListener itemClickListener = new OnItemClickListener() { @Override 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 = ""; if(tgl.isChecked()){ tgl.setChecked(false); strStatus = "Off"; status[position]=false; }else{ tgl.setChecked(true); strStatus = "On"; status[position]=true; } Toast.makeText(getBaseContext(), (String) hm.get("txt") + " : " + strStatus, Toast.LENGTH_SHORT).show(); } }; lvCountries.setOnItemClickListener(itemClickListener); // 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]); hm.put("stat",status[i]); aList.add(hm); } // Keys used in Hashmap String[] from = {"txt","stat" }; // Ids of views in listview_layout int[] to = { R.id.tv_item, R.id.tgl_status}; // 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); lvCountries.setAdapter(adapter); } /** Saving the current state of the activity * for configuration changes [ Portrait <=> Landscape ] */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBooleanArray("status", status); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }
}
5)Now the listview with toggle button is ready








Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
ReplyDeleteandroid development course fees in chennai | android app development training in chennai