Breaking News

How to select all items in ListView and delete all items in Listview


                 


In Android Programming, ListView is used to show something in your activity in a manner.

Some developers as an app need, use a custom list like with TextView, and an Image Button.


But with ListView we use a Contextual menu to handle Long Click on that ListView,

like Select All and Delete all method to use.

Today We Create a Contextual menu in Android with ListView and Select all Button and Delete all button with that.


Create a ListView and find that id. Also, we create an ArrayList and Array adapter to set up our List view. Also, create an int variable called multicount = 0 and String variable called getmap.

 public class Srchvw extends AppCompatActivity {   
     
    DatabaseHelper mydb;
    ListView booklist;
    ArrayList> userlist;
    LinearLayout empty;
    int multicount = 0;
    ArrayAdapter adapter;
    ArrayList> multilist = new ArrayList<>();
    String getmap;  
  @Override   
   protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_book_list);
 
        mydb = new DatabaseHelper(this);
        booklist = findViewById(R.id.booklistview);
        empty = findViewById(R.id.emptyview);
        empty.setVisibility(View.GONE);  

Then create a custom List. create a resource file with three text view. and name it as a book_custom_list


book_custom_list.java

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:padding="5dp">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/custombookid"  
     android:visibility="gone"  
     android:textColor="#343434"  
     android:textSize="14sp"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/custombooktitle"  
     android:textColor="#343434"  
     android:maxLines="2"  
     android:ellipsize="end"  
     android:background="@drawable/multiple_select_item"  
     android:textSize="14sp"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/custombookurl"  
     android:ellipsize="end"  
     android:layout_marginTop="35dp"  
     android:textColor="#343434"  
     android:background="@drawable/multiple_select_item"  
     android:textSize="14sp"/>  
 </RelativeLayout>  

Then, We create a Background Drawable file to change selected items color. Create a Drawable Resource file and name it as a multiple_select_items
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_pressed="true" android:state_activated="true" android:drawable="@color/black_overlay" />  
   <item android:state_activated="true" android:drawable="@color/colorPrimary" />  
   <item android:state_enabled="true" android:drawable="@android:color/transparent" />  
   <item android:drawable="@color/blue_semi_transparent" />  
   
 </selector>  

Then we create a new menu resource file and name it as a book_context_menu

 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto">  
   <item  
     android:id="@+id/select_all"  
     app:showAsAction="always"  
     android:title="Select All"  
     android:orderInCategory="100"  
     />  
   <item  
     android:id="@+id/Delete"  
     app:showAsAction="always"  
     android:title="Delete"  
     android:orderInCategory="100"  
     />  
 </menu>  

now we create a contextual menu


 booklist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        booklist.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
                multicount = booklist.getCheckedItemCount();
                actionMode.setTitle(multicount + " Items Selected");
                if(booklist.isItemChecked(i))
                {
                    multilist.add(userlist.get(i));
                }
                else
                {
                    multilist.remove(userlist.get(i));
                }
            }

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {

                MenuInflater inflater = actionMode.getMenuInflater();
                inflater.inflate(R.menu.book_context_menu,menu);

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case R.id.select_all:

                        for(int i=0 ; i 0)
                            {
                                Toast.makeText(BookList.this, "Deleted", Toast.LENGTH_SHORT).show();
                                mydb.alter();
                                getdata();
                            }
                            else
                            {
                                Toast.makeText(BookList.this, "Error Deleting", Toast.LENGTH_SHORT).show();
                            }
                            adapter.remove(msg);

                        }
                        Toast.makeText(BookList.this, multicount+" items deleted", Toast.LENGTH_SHORT).show();
                        multicount = 0;
                        multilist.clear();

                        actionMode.finish();
                        return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });
    


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

1 comment:

  1. Hello..
    Can you please share your Android Studio file /Source Code file to my email?
    My Email : ricodesrianto@gmail.com

    i'm really need this..
    Thank you before.

    ReplyDelete