Search code examples
androidlistviewandroid-listviewandroid-actionmodeactionmode

Action mode does not start for ListView


When I perform long click on ListView item, action mode does not start. Implementations for ListView are the same through a project:

ListView listView = (ListView) view.findViewById(android.R.id.list);         

listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);              

listView.setMultiChoiceModeListener(new NutritionMultiChoiceModeListener() { 

    @Override                                                                
    public void deleteSelectedItems() {                                      
        for (int i = 0; i < componentAdapter.getCount(); i++) {              
            if (getListView().isItemChecked(i)) {                            
                //Some actions                          
            }                                                                
        }                                                                    
    }                                                                        
});                                                                          

NutritionMultiChoiceModeListener is custom MultiChoiceModeListener, that contains abstract method deleteSelectedItems(), that called for deletions.

Here is layout for ListView item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backround_activated"
android:orientation="vertical" >

<Button
    android:id="@+id/component_button"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/component_grams_edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/ingredient_grams"
    android:inputType="number" />

</LinearLayout>

When I perform long click, I either press the Button or start editing text.

There is only one such place in project. For other item layouts, that contain only TextViews, everything works fine.

Please help.


Solution

  • I don't see where in your code snippet you are starting the action mode on a long-click.

    Here is a sample project that demonstrates starting an action mode on a long-click. In there, the list starts in normal mode, but then on a long-click I switch into the multiple-choice-modal mode and check the long-clicked-upon item:

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view,
                                     int position, long id) {
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setItemChecked(position, true);
    
        return(true);
      }