onContextItemSelected is not triggered when it is called from Dialog window - android

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

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.

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

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

Related

android single item context menu

I have an onLongClickListener set up for a layout. On long click I need a context menu with singe "Delete" option. What is the most simple way to manage that? Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ....
mView = someView;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Menu Title");
MenuItem remove = menu.add("Delete");
remove.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//doStuff...
}
});
super.onCreateContextMenu(menu, v, menuInfo);
}
and in onLongPress or something you can call openContextMenu

How to make a conditional list item in a Android ContextMenu

I have a context menu on a ListView.
I would like to add a special-case context list item for logged in users.
Let's say there is a list of comments. But ONLY for the logged in user's comments, there is a special context list-item called "Edit" (Obviously you don't want other users to be able to edit comments outside of their own.
Here is my class extending application in which I usually check in for logged in users:
public class MyApp extends Application {
public static boolean isUserLoggedIn = false;
public static String username = null;
public static SharedPreferences logInState;
public static int ratescreen = 0;
public static boolean userLogin() {
return MyApp.isUserLoggedIn = true;
}
public static boolean userLogout() {
return MyApp.isUserLoggedIn = false;
}
public static void setUser(String s) {
MyApp.username = s;
}
#Override
public void onCreate() {
super.onCreate();
String PREFS_NAME = "LoginState";
logInState = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
}
}
Here is my context menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.reviews_context, menu);
menu.setHeaderTitle("Mark Comment as ...");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.helpful:
new HelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this up!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.unhelpful:
new UnHelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this down!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.spam:
new SpamTask().execute();
Toast.makeText(getActivity(),
"You have reported this as Spam or Offensive.",
Toast.LENGTH_SHORT).show();
return true;
// Would like to add fourth option here but conditional if it is a comment from the currently logged in user.
}
return false;
}
Simple you add your Edit item depend on user login status in onCreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
// check if user logged-in so add Edit item to context menu.
if (userLogin()) {
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_edit);
}
//Add normall others menu items
menu.add(0, MENU_ITEM_CALL, 0, R.string.menu_callContact);
}
You can cast ContextMenuInfo to AdapterView.AdapterContextMenuInfo for ListView than change its content accessing to adapter (your own implementation).
AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo)menuInfo;
MyAdapter adapter = (MyAdapter)getListView().getAdapter();
adapter.remove(contextMenuInfo.position);

onContextItemSelected Error

I having some difficulty implementing a context menu into my android application. My first problem was I was trying to implement OnCreateContextMenu inside of OnCreate but I kept getting an error saying:
void is an invalid type for the variable onCreateContextMenu
I fixed this problem by putting onCreateContextMenu outside of OnCreate. Now my problem lies with OnContextItemSelected. My error occurs on the line: public boolean onContextItemSelected(MenuItem menu). The errors are:
implements android.view.View.OnLongClickListener.onLongClick
Syntax error, insert "}" to complete MethodBody
Here is the code:
BaconStripsButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = 0)
{
Toast ringtone = Toast.makeText(startingPoint.this, "Ringtone added Successfully!", Toast.LENGTH_SHORT);
return true;
}
return false;
}
}
});
Any help would be appreciated. Thanks, Justin
No need to use onContextItemSelected Inside onlongClick of button.Just Override OnContextItemSelected(); and register ContextMenu to btn.No need to setOnlongClickListener.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Then override
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then finally registerContextMenu(button);
You may use the ListView and implement the onCreateContextMenu in the OnCreate of the Activity.
like this:
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// add some sublist
menu.setHeaderTitle(R.string.collect_title);
menu.add(0, 1, 0, R.string.delete_string);
menu.add(0, 2, 0, R.string.move_to_project_string);
menu.add(0, 3, 0, R.string.move_to_action_string);
}
});

Context menu on extended LinearLayout view

I've a class (a view) that extends LinearLayout.
Now, I'm trying to do a context menu for each element of that class in my activity, but there's no way, it never fires:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
MyLinearLayout[] arrayLayout = new MyLinearLayout[num];
for (int i = 0; i < arrayLayout.length; i++)
{
//I ve removed code that fill the array and do some actions
//and then:
ll.addView(arrayLayout[i]);
registerForContextMenu(arrayLayout[i]);
}
setContentView(ll);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Log.i(LOGTAG, "context menu");
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, R.string.CTX_EDIT);
menu.add(0, v.getId(), 0, R.string.CTX_ELIM);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
function1(item.getItemId());
return true;
}
public void function1(int id){
Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
}
The context menu never shows, i've read a lot problems about it, and i think this is because of the LinearLayout extended class, but I'm sure there is a way to do this, or i'm missing something. Can anyone help, please? thanks!
You are creating an empty array of MyLinearLayout: arrayLayout = new MyLinearLayout[num],
Then you are trying to set a context menu with:
registerForContextMenu(arrayLayout[i])
Which does nothing since arrayLayout[i] is null.

display context menu for position "2" click in list view only android

I want to display context menu for list position clicked means only for 2 nd position clicked in list view so how to do it.
I had implemented the code for displaying context menu for each position clicked. So how to make it specific for any position in ListView.
my code for displaying context menu for each position in list view ..pls do required modification on my code thanks...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenulist);
// registerForContextMenu(getListView());
CustomAdapter adapter = new CustomAdapter(
this, R.layout.listitem,R.id.title, data
);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
#Override
public void onCreateContextMenu(ContextMenu menu,
View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Gallery");
menu.add(0, v.getId(), 0, "Camera");
menu.add(0, v.getId(), 0, "Cancel");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Gallery"){
function1(item.getItemId());
} else if(item.getTitle()=="Camera"){
function2(item.getItemId());
} else return false;
return true;
}
public void function1(int id){
Toast.makeText(this, "Gallery function called",
Toast.LENGTH_SHORT)
.show();
}
public void function2(int id){
Toast.makeText(this, "Camera function called",
Toast.LENGTH_SHORT)
.show();
}
In your code I can see only onCreateOptionsMenu() method. In order to restrict the options menu as you wish, you have to add one more method to it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
And now when you click on the listview get the position of the item that is being clciked and store it globally. Now change the above method like this,
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(list_item_position==2){
return true;
}
else
return false;
}
Now your options menu will pop up only if the list item in second position is clicked.

Categories

Resources