eve
eve

Reputation: 71

Context menu by short click

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);
    }
}

Upvotes: 7

Views: 6190

Answers (3)

Amatirta Angelo
Amatirta Angelo

Reputation: 1

Don't forget to add this after registerForContextMenu() to disable long click:

listview.setLongClickable(isRestricted());

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

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.

Upvotes: 7

slund
slund

Reputation: 6397

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.

Upvotes: 5

Related Questions