Android: Context menu on single click

I have an Intent that extends a ListActivity. In my onCreate method after having populated the list adapter I use registerForContextMenu(getListView()); to register for a context menu.

Now it is working and the context menu has its original function which is; once I click and hold down on an item the context menu opens.

Can I open the context menu on a single click (without having to hold down on the list)?

All help is appreciated.


call activity.openContextMenu(l) onitem click event to open contextmenu on single click and onLongClick call activity.closeContextMenu()

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyListView extends ListActivity implements OnItemLongClickListener {
    /** Called when the activity is first created. */
    Activity activity = null;
    public void onCreate(Bundle savedInstanceState) {

        activity = this;

        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, PENS);

        ListView lv = getListView();



    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub

        System.out.println("...11configuration is changed...");

    static final String[] PENS = new String[]{
        "MONT Blanc",
        "Porsche Design",


    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        System.out.println("...context is called");


    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        System.out.println("...on create context menu...");
        super.onCreateContextMenu(menu, v, menuInfo);

    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        System.out.println("...on long click close context menu...");
        // TODO Auto-generated method stub

        return false;

Here is another simpler way to show context menu on single click.

private void addOnClickListener()
    contactList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)

replace contactList by your ListView and make sure to call this method after the initialization of ListView.

I don't think it's working smoothly. Calling openContextMenu(l) will cause item.getMenuInfo() to be null (inside method onContextItemSelected(MenuItem item)).

You should call l.showContextMenuForChild(v) instead of openContextMenu(l).

this work perfect....

    listmp3 = (ListView) findViewById(;

    listmp3.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Need Your Help

Adjacent divs with angled borders?

html css html5 css-shapes

I want to create two divs which are floated left to each other, however with a slanted angled border separating them. I've attached a picture to demonstrate what I mean.

Why does dplyr's filter drop NA values from a factor variable?

r dplyr subset na

When I use filter from the dplyr package to drop a level of a factor variable, filter also drops the NA values. Here's an example: