This example will show you how to add a checkbox to each android listview item. It also demonstrates how to select all, deselect all, reverse select and remove selected listview items programmatically.
1. Android ListView With CheckBox Example.
- Below is this example demo video ( select all none reverse and delete selected list view items example ).
- There are four Java classes, one main layout XML file, and one list view item layout XML file in the above example.
./ ├── app │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ └── dev2qa │ │ │ └── example │ │ │ ├── view │ │ │ │ ├── listview │ │ │ │ │ └── checkbox │ │ │ │ │ ├── ListViewItemCheckboxBaseAdapter.java │ │ │ │ │ ├── ListViewItemDTO.java │ │ │ │ │ ├── ListViewItemViewHolder.java │ │ │ │ │ └── ListViewWithCheckboxActivity.java │ │ ├── res │ │ │ ├── layout │ │ │ │ ├── activity_list_view_with_checkbox.xml │ │ │ │ ├── activity_list_view_with_checkbox_item.xml
1.1 Main Activity Java File.
- ListViewWithCheckboxActivity.java
package com.dev2qa.example.view.listview.checkbox; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.ListView; import com.dev2qa.example.R; import java.util.ArrayList; import java.util.List; public class ListViewWithCheckboxActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_with_checkbox); setTitle("dev2qa.com - Android ListView With CheckBox"); // Get listview checkbox. final ListView listViewWithCheckbox = (ListView)findViewById(R.id.list_view_with_checkbox); // Initiate listview data. final List<ListViewItemDTO> initItemList = this.getInitViewItemDtoList(); // Create a custom list view adapter with checkbox control. final ListViewItemCheckboxBaseAdapter listViewDataAdapter = new ListViewItemCheckboxBaseAdapter(getApplicationContext(), initItemList); listViewDataAdapter.notifyDataSetChanged(); // Set data adapter to list view. listViewWithCheckbox.setAdapter(listViewDataAdapter); // When list view item is clicked. listViewWithCheckbox.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int itemIndex, long l) { // Get user selected item. Object itemObject = adapterView.getAdapter().getItem(itemIndex); // Translate the selected item to DTO object. ListViewItemDTO itemDto = (ListViewItemDTO)itemObject; // Get the checkbox. CheckBox itemCheckbox = (CheckBox) view.findViewById(R.id.list_view_item_checkbox); // Reverse the checkbox and clicked item check state. if(itemDto.isChecked()) { itemCheckbox.setChecked(false); itemDto.setChecked(false); }else { itemCheckbox.setChecked(true); itemDto.setChecked(true); } //Toast.makeText(getApplicationContext(), "select item text : " + itemDto.getItemText(), Toast.LENGTH_SHORT).show(); } }); // Click this button to select all listview items with checkbox checked. Button selectAllButton = (Button)findViewById(R.id.list_select_all); selectAllButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int size = initItemList.size(); for(int i=0;i<size;i++) { ListViewItemDTO dto = initItemList.get(i); dto.setChecked(true); } listViewDataAdapter.notifyDataSetChanged(); } }); // Click this button to disselect all listview items with checkbox unchecked. Button selectNoneButton = (Button)findViewById(R.id.list_select_none); selectNoneButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int size = initItemList.size(); for(int i=0;i<size;i++) { ListViewItemDTO dto = initItemList.get(i); dto.setChecked(false); } listViewDataAdapter.notifyDataSetChanged(); } }); // Click this button to reverse select listview items. Button selectReverseButton = (Button)findViewById(R.id.list_select_reverse); selectReverseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int size = initItemList.size(); for(int i=0;i<size;i++) { ListViewItemDTO dto = initItemList.get(i); if(dto.isChecked()) { dto.setChecked(false); }else { dto.setChecked(true); } } listViewDataAdapter.notifyDataSetChanged(); } }); // Click this button to remove selected items from listview. Button selectRemoveButton = (Button)findViewById(R.id.list_remove_selected_rows); selectRemoveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog alertDialog = new AlertDialog.Builder(ListViewWithCheckboxActivity.this).create(); alertDialog.setMessage("Are you sure to remove selected listview items?"); alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Confirm", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { int size = initItemList.size(); for(int i=0;i<size;i++) { ListViewItemDTO dto = initItemList.get(i); if(dto.isChecked()) { initItemList.remove(i); i--; size = initItemList.size(); } } listViewDataAdapter.notifyDataSetChanged(); } }); alertDialog.show(); } }); } // Return an initialize list of ListViewItemDTO. private List<ListViewItemDTO> getInitViewItemDtoList() { String itemTextArr[] = {"Android", "iOS", "Java", "JavaScript", "JDBC", "JSP", "Linux", "Python", "Servlet", "Windows"}; List<ListViewItemDTO> ret = new ArrayList<ListViewItemDTO>(); int length = itemTextArr.length; for(int i=0;i<length;i++) { String itemText = itemTextArr[i]; ListViewItemDTO dto = new ListViewItemDTO(); dto.setChecked(false); dto.setItemText(itemText); ret.add(dto); } return ret; } }
1.2 ListView Custom Adapter Java File.
- ListViewItemCheckboxBaseAdapter.java
package com.dev2qa.example.view.listview.checkbox; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; import com.dev2qa.example.R; import java.util.List; public class ListViewItemCheckboxBaseAdapter extends BaseAdapter { private List<ListViewItemDTO> listViewItemDtoList = null; private Context ctx = null; public ListViewItemCheckboxBaseAdapter(Context ctx, List<ListViewItemDTO> listViewItemDtoList) { this.ctx = ctx; this.listViewItemDtoList = listViewItemDtoList; } @Override public int getCount() { int ret = 0; if(listViewItemDtoList!=null) { ret = listViewItemDtoList.size(); } return ret; } @Override public Object getItem(int itemIndex) { Object ret = null; if(listViewItemDtoList!=null) { ret = listViewItemDtoList.get(itemIndex); } return ret; } @Override public long getItemId(int itemIndex) { return itemIndex; } @Override public View getView(int itemIndex, View convertView, ViewGroup viewGroup) { ListViewItemViewHolder viewHolder = null; if(convertView!=null) { viewHolder = (ListViewItemViewHolder) convertView.getTag(); }else { convertView = View.inflate(ctx, R.layout.activity_list_view_with_checkbox_item, null); CheckBox listItemCheckbox = (CheckBox) convertView.findViewById(R.id.list_view_item_checkbox); TextView listItemText = (TextView) convertView.findViewById(R.id.list_view_item_text); viewHolder = new ListViewItemViewHolder(convertView); viewHolder.setItemCheckbox(listItemCheckbox); viewHolder.setItemTextView(listItemText); convertView.setTag(viewHolder); } ListViewItemDTO listViewItemDto = listViewItemDtoList.get(itemIndex); viewHolder.getItemCheckbox().setChecked(listViewItemDto.isChecked()); viewHolder.getItemTextView().setText(listViewItemDto.getItemText()); return convertView; } }
1.3 Custom ListView Item ViewHolder Java File.
- ListViewItemViewHolder.java
package com.dev2qa.example.view.listview.checkbox; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; public class ListViewItemViewHolder extends RecyclerView.ViewHolder { private CheckBox itemCheckbox; private TextView itemTextView; public ListViewItemViewHolder(View itemView) { super(itemView); } public CheckBox getItemCheckbox() { return itemCheckbox; } public void setItemCheckbox(CheckBox itemCheckbox) { this.itemCheckbox = itemCheckbox; } public TextView getItemTextView() { return itemTextView; } public void setItemTextView(TextView itemTextView) { this.itemTextView = itemTextView; } }
1.4 ListView Item Data DTO Java File.
- ListViewItemDTO.java
package com.dev2qa.example.view.listview.checkbox; public class ListViewItemDTO { private boolean checked = false; private String itemText = ""; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public String getItemText() { return itemText; } public void setItemText(String itemText) { this.itemText = itemText; } }
1.5 Main Layout XML File.
- activity_list_view_with_checkbox.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@+id/list_view_with_checkbox" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/list_select_all" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select All" /> <Button android:id="@+id/list_select_none" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select None"/> <Button android:id="@+id/list_select_reverse" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Reverse Select"/> <Button android:id="@+id/list_remove_selected_rows" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Remove Selected Rows" /> </LinearLayout>
1.6 ListView Item Layout XML File.
- activity_list_view_with_checkbox_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/list_view_item_checkbox" android:width="0dp" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false"/> <TextView android:id="@+id/list_view_item_text" android:layout_weight="9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> </LinearLayout>
I’m trying to build up this example and am stuck in the xml file
where it says ‘Namespace prefix Android is not defined’. (Using Visual Studio 2019)
I am trying to get the clicked items by updating the initItemList checkbox, but when the process is entering the delete button, the initItemList checked variable is not updated to “true.”
I had to set the clickable and focusable property of the checkbox in the row.xml to “true,” otherwise the UI is not clickable.
Thanks.
I tried to run and I get this error any help
Process: com.example.checkbox, PID: 28511
java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.CheckBox.setChecked(boolean)’ on a null object reference
at com.example.checkbox.ListViewItemCheckboxBaseAdapter.getView(ListViewItemCheckboxBaseAdapter.java:78)
How to add items to listview
filter ?
How to get selected items from this list
Please see the method
// When list view item is clicked.
listViewWithCheckbox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int itemIndex, long l)
in ListViewWithCheckboxActivity.java
hey,
it seems I cannot resolve symbol widget.recycler, thus “extends recycler.viewholder” won’t work; I am new to all of this, could you please help me solve this?
What error message can you get?
add implementation ‘com.android.support:recyclerview-v7:26.1.0’
or higher version to your build.grade 🙂
I want to make a single choice listview. How can I do it?
Please refer below page, https://stackoverflow.com/questions/8337180/custom-single-choice-listview