14
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..

Sai
  • 15,188
  • 20
  • 81
  • 121

4 Answers4

7

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().

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I followed exactly what you told but Still the `onContextItemSelected` not calling but a very thanks to your reply. but as u can see in my reply, after i select some item in `onCreateContextMenu` the flow of code is going to `onMenuItemSelected` @Sunil – Sai Apr 18 '15 at 09:55
  • Does it open the context menu in the onClick() listener? also, you need to use just findViewById() - not dialog.findViewById(). I'll edit this in my answer. But can you tell me if the context menu opens when you expect it to? – ucsunil Apr 18 '15 at 10:05
  • Yes the context menu opens first. I have removed the dialog.findviewById. I have updated my reply with full code. please have a check.. – Sai Apr 18 '15 at 10:18
  • I overlooked this line of code and I found it after the other answer was posted. I have edited my answer to describe the solution and the line ringtone_layout.setOnCreateContextMenuListener(this); – ucsunil Apr 18 '15 at 10:35
  • Yes, even I was thinking the same, thanks you very much for response and care. – Sai Apr 18 '15 at 10:52
  • I have one more doubt, you told " Avoid using System.out.println() - instead use Android Logging " can you explain me short ? – Sai Apr 18 '15 at 10:56
  • 1
    System.out.println() is used to print messages to a console which Android technically does not have. In newer devices and emulators Android is smart enough to know that it should send it to the logcat but older devices might not do this and it might get lost. Android logging is built for this purpose - since you do not have a traditional console to view Android messages, you should not use this. It might work but it's not advisable – ucsunil Apr 18 '15 at 10:59
4

This issue already solved in the following SO threads: onContextItemSelected doesn't get called and onContextItemSelected never called using a Dialog with a ListView.

If none of the answers helped you, try this: return false in onContextItemSelected method in your activity and in its fragments.

Community
  • 1
  • 1
Zsolt Mester
  • 1,053
  • 9
  • 14
  • I have already check those answers, I'm not using list view, I'm passing Relativelayout for registeringcontextmenu. My problem itself the `onContextItemSelected ` is not being called but you are telling me to add a retune statement to it. How will it make sense. Anyway thanks for your reply. – Sai Apr 18 '15 at 10:38
  • It have sence, because the activity or its fragments can catch the onContextItemSelected before you can catch it. – Zsolt Mester May 03 '15 at 11:52
1
public class MusicsetDialog extends Dialog implements OnClickListener {
    private RelativeLayout ringtone_layout;

    public MusicsetDialog(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_popup);
        setTitle(R.string.msettings);

        ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);

        ringtone_layout.setOnClickListener(MusicsetDialog.this);

        registerForContextMenu(ringtone_layout);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ringtone_layout:
            openContextMenu(ringtone_layout);
            break;          
        default:
            break;
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select The Action");
        // groupId, itemId, order,title
        menu.add(0, v.getId(), 0, "Edit");
        menu.add(0, v.getId(), 1, "Delete");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @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) {
        // System.out.println("onContextItemSelected");
        Log.d("MusicsetDialog", "onContextItemSelected");
        return super.onContextItemSelected(item);
    }
}

I have created a subclass for mydialog and extended Dialog and followed the procedure of Sunil. But still onContextItemSelected was not called. so after doing some research, I Override onMenuItemSelected and passed the selected MenuItem to onContextItemSelected and it worked fine. may be i'm calling onContextItemSelected forcefully. I don't know. I'm new to android, any explanation why onContextItemSelected is still not called automatically would be appreciated .. Thanks..

Sai
  • 15,188
  • 20
  • 81
  • 121
  • You are indeed force calling the onContextItemSelected(). Is the context menu even coming up in the first place? – ucsunil Apr 18 '15 at 10:11
  • 1
    I have updated my solution. You do not need to use the onContextItemSelected() method. Does not look like it is called when you click the context menu item – ucsunil Apr 18 '15 at 10:43
1

Try this

new AlertDialog.Builder(this) 
        .setSingleChoiceItems(items, 0, null) 
        .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                // Do something useful withe the position of the selected radio button 
            } 
        }) 
        .show(); 
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32