jph
jph

Reputation: 2233

how to avoid both onItemClick and onCreateContextMenu from being called

My Activity uses a ListView. I would like to do X when the user long-taps an item and Y when he normal-taps. My onCreate() contains code like:

mListView = (ListView) findViewById(android.R.id.list);
registerForContextMenu(mListView);
mListView.setOnItemClickListener(this);

The behavior I'm seeing is that when the user long-taps on an item, both onCreateContextMenu() and onItemClick() are called.

How can I avoid this?

Upvotes: 1

Views: 774

Answers (2)

Smalls
Smalls

Reputation: 374

Setting the OnItemLongClickListener to null for the list view seems to do the trick, leaving the long press handling to show the context menu:

mListView = (ListView) findViewById(android.R.id.list);
registerForContextMenu(mListView);
mListView.setOnItemClickListener(this);
mListView.setOnItemLongClickListener(null);

If this feels too hacky to set the OnItemLongClickListener to null, you can also set it to an OnLongClickListener that returns false, signaling that the long click was not consumed.

mListView.setOnLongClickListener(new OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    return false;
  }
});

Upvotes: 0

Amit Hooda
Amit Hooda

Reputation: 2144

Try this, I have tested it and context menu is called only on long press and onclick only listview click item event takes place

package com.example.listview;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListViewExampleActivity extends ListActivity {

static final String[] STATES = new String[] { "Haryana", "Punjab", "Rajasthan",
        "Maharashtra", "Madhya Pradesh", "Kerala", "Jammu","Bihar","Karnataka" , "TamilNadu",
                "Uttar Pradesh" ,"Gujrat"};
View listColor;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.main,STATES));

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

    listView.setBackgroundColor(getTitleColor());
    registerForContextMenu(listView);
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            if(listColor!=null){
                listColor.setBackgroundColor(Color.BLACK);
                listColor=view;
            }else{

                listColor=view;

            }
            Log.i("called", "item click");
            listColor.setBackgroundColor(Color.BLUE);
           //on click of any item the item name will be shown in toast
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    Log.i("called", "Context menu");
    Toast.makeText(getApplicationContext(),
            "Context menu", Toast.LENGTH_SHORT).show();
}
}

Upvotes: 0

Related Questions