To show a context menu on long click, you should call registerForContextMenu(View)
for view that is clicked.
You should also override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)
You not need setOnLongClickListener
. If you need it for some other reason, it should return false.
In your code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
registerForContextMenu(btn1);
btn1.setOnClickListener(new View.OnClickListener() {
-------------
-------------
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
To handle the context menu:
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.contextItem1:
//Do what you want
return true;
case R.id.contextItem2:
//Do what you want
return true;
default:
return super.onContextItemSelected(item);
}
}