Android Example to Enable Multi-item Selection in ListView

In this there is no need to modify the main.xml file to include the ListView.The ListActivity class itself contains a ListView.

mainActivity.java

package com.example.scanfcode;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity
{
	String[] scanf={"C++","Java","Android","Python","Html","Css","Javascript","Php"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		ListView l1=getListView();
		l1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		l1.setTextFilterEnabled(true);
		setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,scanf));
	}

	

	public void onListItemClick(ListView p1,View v,int pos,long id)
	{
		Toast.makeText(getApplicationContext(), scanf[pos],Toast.LENGTH_SHORT).show();
	}

}

OUTPUT:-

listView

Leave a Reply

Your email address will not be published. Required fields are marked *