Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context menu by short click

Tags:

android

Sorry for stupid question, but what I should change/add in that code- to show context menu by short click on item in list view?

public class MyActivity extends ListActivity implements AdapterView.OnItemClickListener {
    static final String[] COUNTRIES = new String[]{
            "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
            "Angola", "Anguilla", "Antarctica"
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(this);
    }


    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        Log.e("sdklfjsdkljfl", " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ");
        menu.setHeaderTitle("HELLO");

    }

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("kjhasjkdhkas", "sdkhjkhskaf");
        this.openContextMenu(view);
    }
}
like image 855
eve Avatar asked May 05 '11 23:05

eve


People also ask

What is context menu shortcut?

In Microsoft Windows, pressing the Application key or Shift+F10 opens a context menu for the region that has focus.

How do I select from context menu?

Hold down Ctrl and right click. This brings up a selection-conversion-related context menu. You can convert selections to different selection modes (such as verticies, UVs, faces, etc.), or alter the existing selection to include more (or fewer) of the same elements.

Where is context menu in Word?

The context menu may also be called from the keyboard, activating wherever the cursor or current text selection is, either by pressing the context menu button, (present on some keyboards, between the right side alt and ctrl keys,) or by activating the windows shortcut, Shift-F10.

Where is the context on keyboard?

Did you know your keyboard has a menu key? On full-size keyboards, you'll find it to the left of your right Ctrl key. This key opens context menus, but you can tweak it to make it more useful.


2 Answers

The other solutions posted here didn't work for me because I was using a ListFragment. However the following solutions seems to work quite nicely for both a ListFragment and a ListActivity (or just any old listview in general):

public void onListItemClick(ListView l, View v, int position, long id){
  l.showContextMenuForChild(v);   
}

It's much more simpler and elegant. In fact, this is actually how the ListView class itself initiates the context menu on a long click.

like image 73
Kurtis Nusbaum Avatar answered Nov 23 '22 06:11

Kurtis Nusbaum


You need to call registerForContextMenu on the view.

EDITED to add call to setLongClickable(false)

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    registerForContextMenu( view ); 
    view.setLongClickable(false);  // undo setting of this flag in registerForContextMenu
    this.openContextMenu(view);
}

You will also need to actually add menu items or the menu won't show either. Just setting the header is not enough.

NOTE: I have not completely tracked this down yet, but calling registerForContextMenu(view) sets a flag assuming that you want the context menu on a long press. When this flag is set, the onTouch logic in AbsListView is somehow no longer firing onClick. I don't have time to dig through this completely. It would appear though that when using a simple adapter like an ArrayAdapter, and using a ListActivity with the default ListView, you will need to decide between having context menu appear on short click, or being able to use longclick.

If you are not interested in long presses, you can get your context menu working on short press by undoing the flag set in registerForContextMenu( view );

Perhaps someone else has more info or more time to dig through the code.

like image 38
slund Avatar answered Nov 23 '22 06:11

slund