Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to Highlight selected item in List View and create Contextual menu



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 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.
 public class Srchvw extends AppCompatActivity {   
     
   ListView searchlist;   
   int multicount = 0;   
   ArrayList<String> list = new ArrayList<>();   
   ArrayAdapter adapter;   
  @Override   
   protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_srchvw);   
    searchlist = findViewById(R.id.SearchListView);   

Then create a custom List. create a resource file with only a text view. and name it as a searchcustomlist
 <?xml version="1.0" encoding="utf-8"?>  
   
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@android:id/text1"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:textAppearance="?android:attr/textAppearanceListItemSmall"  
   android:gravity="center_vertical"  
   android:paddingLeft="3dp"  
   android:paddingRight="3dp"  
   android:background="@drawable/multiple_select"  
   android:minHeight="?android:attr/listPreferredItemHeightSmall" />  
   

Here We create a Background Drawable file to change selected items color. Create a Drawable Resource file and name it as a multiple_select
 <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>  

now add some values to your custom list and set up your adapter with this custom list.
 list.add("Harpreet");  
     list.add("Studio");  
     list.add("hello");  
     list.add("today");  
     list.add("we create");  
     list.add("Searchview");  
     list.add("search");  
     list.add("something");  
     list.add("in your list");  
   
     adapter = new ArrayAdapter<String>(this, R.layout.searchcustomlist,list);  
     searchlist.setAdapter(adapter);  


Now we create a menu file to show in our contextual menu
create a new menu resource file and name it as a srch_context_menu
 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android">  
   
   <item  
     android:id="@+id/select"  
     android:title="Select_ALL"  
     />  
 </menu>  

now we create a contextual menu
 searchlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        searchlist.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
                 multicount = searchlist.getCheckedItemCount();
                actionMode.setTitle(multicount + " items selected");
                if(searchlist.isItemChecked(i))
                {
                    multicount = multicount+1;
                }
                else
                {
                    multicount = multicount - 1;
                }

            }

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

                MenuInflater inflater = actionMode.getMenuInflater();
                inflater.inflate(R.menu.srch_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:

                        for ( int i=0; i < searchlist.getAdapter().getCount(); i++)
                        {
                            searchlist.setItemChecked(i, true);
                        }
                        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. The King Casino - CommunityKhabar
    The King Casino is the only casino near the casino. All casino communitykhabar games are ventureberg.com/ legal septcasino and the game https://deccasino.com/review/merit-casino/ variety is huge! The games are also available at any of the

    ReplyDelete