Dialog dialog;
private void opendialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) dialog
.findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) dialog
.findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
System.out.println("Inside onContextItemSelected");
return super.onContextItemSelected(item);
}
onContextItemSelected is never called when use context menu inside a Dialog. Is there any thing wrong with my code ? Thanks in advance..
NOTE: Since this answer seems to be getting some attention (upvotes), I am editing the code snippet to reflect a more concise answer to the question.
You are trying to register for the context menu for a view item within the dialog but from the activity. This approach is wrong. You actually need to subclass Dialog and then create and expand your views there and then override the onCreateContextMenu() there to do your work and register your view for the context menu. You should then create an instance of that dialog here. So it will be something like:
public class Mydialog extends Dialog {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
// You should do the processing for the selected context item here. The
// selected context item gets passed in the MenuItem parameter in
// the following method. In my answer I am force calling the onContextItemSelected()
// method but you are free to do the actual processing here itself
@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// Avoid using System.out.println() - instead use Android Logging
return super.onContextItemSelected(item);
}
}
Now, you can create an instance of this dialog and your views will have the context item registered successfully. So your openDialogMethod() will now look like:
private void opendialog() {
dialog = new MyDialog(MainActivity.this);
// the context menu will now be registered
dialog.show();
}
Although you were originally passing the context of the activity to the Dialog that you were creating, you cannot pass on the context menu creation listeners like that. To do that you will have to subclass your dialog as I have shown above.
EDIT: After looking at Zsolt's answer, I found out that I overlooked this line of code that you did not have. You need to have:
ringtone_layout.setOnCreateContextMenuListener(this);
What you say about forcefully calling the context menu is actually true. You are just calling the regular menu and then you are passing that id on to the context menu callback. The if clause passes because the id is from the context menu feature.
And after some further reading, it looks like you are supposed to do your menu handling on onMenuItemSelected() and not in onContextItemSelected(). So what you have now is correct and you do not need to forcefully call the other method. Just do your processing in onMenuItemSelected().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With