1

I want to show ActionMode menu (for having more options) with LongClicking on each rows of my ListView in my proje. but as I click on them, nothing happen. note: I use this tutorial for writing this code: http://wptrafficanalyzer.in/blog/creating-a-contextual-menu-bar-contextual-action-mode-for-a-single-view-in-android/ this is my code:

public class MyActivity extends Activity  {

private ListView listView;

private ActionMode actionMode;
 ActionMode.Callback callback;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.ListView);


    final ActionMode.Callback callback = new ActionMode.Callback() {
        /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }


        /** Called when user exits action mode */
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            actionMode = null;

        }

        /** This is called when the action mode is created. This is called by startActionMode() */
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Demo");
            getMenuInflater().inflate(R.menu.edit_vow, menu);
            return true;
        }

        /** This is called when an item in the context menu is selected */
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.editBTN:
                    Toast.makeText(getBaseContext(), "Selected Action1 ", Toast.LENGTH_LONG).show();
                    mode.finish();  // Automatically exists the action mode, when the user selects this action
                    break;
                case R.id.deleteBTN:
                    Toast.makeText(getBaseContext(), "Selected Action2 ", Toast.LENGTH_LONG).show();
                    break;
                case R.id.doneBTN:
                    Toast.makeText(getBaseContext(), "Selected Action3 ", Toast.LENGTH_LONG).show();
                    break;
            }
            return true;
        }
    };

    //long-click to edit data
     View.OnLongClickListener listener = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if(actionMode!=null)
                return false;
            else
                actionMode = startActionMode(callback);
            return true;
        }
    };

    listView.setOnLongClickListener(listener);


}

Please help me, thanks!

Unheilig
  • 16,196
  • 193
  • 68
  • 98

1 Answers1

2

I think you can look at the Android demo in Android SDK samples. You can find the answer in List15.java in ApiDemos projects in Android SDK samples. The sample is:`

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.example.android.apis.view;

 import com.example.android.apis.R;

 import android.app.ListActivity;
 import android.os.Bundle;
 import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Toast;

 /**
  * This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a.                selection mode on ListView.
  */
 public class List15 extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv.setMultiChoiceModeListener(new ModeCallback());
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, mStrings));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getActionBar().setSubtitle("Long press to start selection");
}

private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_select_menu, menu);
        mode.setTitle("Select Items");
        setSubtitle(mode);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.share:
            Toast.makeText(List15.this, "Shared " + getListView().getCheckedItemCount() +
                    " items", Toast.LENGTH_SHORT).show();
            mode.finish();
            break;
        default:
            Toast.makeText(List15.this, "Clicked " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        setSubtitle(mode);
    }

    private void setSubtitle(ActionMode mode) {
        final int checkedCount = getListView().getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setSubtitle(null);
                break;
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + checkedCount + " items selected");
                break;
        }
    }
}

    private String[] mStrings = Cheeses.sCheeseStrings;
}

`

and you can try this with your code

`

    public class MyActivity extends Activity  {

private ListView listView;

ActionMode.Callback callback;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     listView = (ListView) findViewById(R.id.ListView);
     listView .setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
     listView .setMultiChoiceModeListener(callback);

     final ActionMode.Callback callback = new ActionMode.Callback() {
         /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
 @Override
 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
     return true;
 }


 /** Called when user exits action mode */
 @Override
 public void onDestroyActionMode(ActionMode mode) {

 }

 /** This is called when the action mode is created. This is called by startActionMode() */
 @Override
 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
     mode.setTitle("Demo");
     getMenuInflater().inflate(R.menu.edit_vow, menu);
     return true;
 }

 /** This is called when an item in the context menu is selected */
 @Override
 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
     switch(item.getItemId()){
         case R.id.editBTN:
             Toast.makeText(getBaseContext(), "Selected Action1 ", Toast.LENGTH_LONG).show();
             mode.finish();  // Automatically exists the action mode, when the user selects this action
             break;
         case R.id.deleteBTN:
             Toast.makeText(getBaseContext(), "Selected Action2 ", Toast.LENGTH_LONG).show();
             break;
         case R.id.doneBTN:
             Toast.makeText(getBaseContext(), "Selected Action3 ", Toast.LENGTH_LONG).show();
                     break;
             }
             return true;
         }
     };
}`
Umair
  • 6,366
  • 15
  • 42
  • 50
windleafs
  • 19
  • 3
  • @windleafs.you mean that I must change all my ListView codes? Or just use ModeCallback.java class? –  Mar 29 '15 at 13:20
  • @windleafs.i do so.but as I click on list view's items, nothing happens(action mode does not show) and as I long-click on them, "the app has been stopped" shows. what should I do?! please help me. –  Apr 02 '15 at 14:46
  • Can you give me your project? And my gmail is windleafs@gmail.com – windleafs Apr 02 '15 at 15:20
  • Excuse me,Did you recieve my email with "mina dahesh. AAction mode android" subject? –  Apr 05 '15 at 07:03