Hiding context menu items - android

There is a context menu in my application, but I want to hide its items when a particular condition is specified.
What should I do ?
This is onCreateContextMenu code
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select the Action");
menu.add(0,0,getAdapterPosition(), Common.EDIT_POST);
menu.add(0,1,getAdapterPosition(),Common.DELETE_POST);
}

Create a global variable. Change its state when your condition is met. Do not add the menu items you want to hide to menu.
// Declare as global inside Activity
private customCondition = true;
...
// Check if your condition has been met and change the variable state
if(isConditionMet()) {
customCondition = false;
} else {
customCondition = true;
}
...
Now inside onCreateContextMenu,
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select the Action");
menu.add(0, 0, getAdapterPosition(), Common.EDIT_POST);
if(!customCondition) {
// Hide the delete post menuitem
menu.add(0, 1, getAdapterPosition(),Common.DELETE_POST);
}
}

Related

replace of menu item position by another menu item position in same menu

I am trying to create a context menu for card view.Initial menu like
fig 1: initial context menu
and i need to replace it like fig 2: replaced context menu
.When i click on disable menu the card view will be disabled,and the disable menu should replace with enable menu
call invalidateOptionsMenu() after click on menu item to change menu item title.
Boolean IsEnable = false;
#Override
public boolean onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem reminderstatus = (MenuItem) menu.findItem(R.id.reminderstatus);
if (IsEnable) {
reminderstatus.setTitle("Disable");
} else {
reminderstatus.setTitle("Enable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (IsEnable) {
IsEnable = false;
} else {
IsEnable = true;
}
invalidateOptionsMenu();
}
}
Actually you don't need to create Different Menu item for Enable and Disable , take a single menu-item , just change the text of menu item , from their Status (Enable or Disable).
hope you are saving status that currently it is Enable or Disable.
#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);
// Select a menu item then change it's title (text)
MenuItem mi = (MenuItem) menu.findItem(R.id.YOUR_MENU_ID);
if(CHECK_YOUE_CURRENT_STATUS_HERE){
//SET YOUR CURRENT STATUS ACCORDINGLY CURRENT STATUS (ENABLE /DISABLE)
mi.setTitle("Enable/Disable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (YOUR_CURRENT_STATUS) {
YOUR_CURRENT_STATUS = false;
} else {
YOUR_CURRENT_STATUS = true;
}
invalidateOptionsMenu();
//this method refresh your Context menu view so basically call
// your onCreateContextMenu once again which will check for your
// Status and set accordingly it.
}
}
Just check this sample code implement it correctly will work for you.

onContextItemSelected is not triggered when it is called from Dialog window

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

Detecting onContextMenu clicks in a fragment

So I've successfully connected a context menu pop up to a list view in a fragment. The items show up, but when I click on them, onContextMenuItemSelectedMenu() is ignored and instead onMenuItemClick() is called in the parent activity. How can I make it so when I click the context menu items onContextMenuItemSelectedMenu() is called in the fragment instead. Thanks.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item1");
menu.add("item2");
menu.add("item3");
}
#Override
public boolean onContextItemSelected (android.view.MenuItem item){
Log.i("cTest", "clicked context menu");
return true;
}
I figured it out. It's turned out to be the same as buttons. Both in the fragment:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item0").setOnMenuItemClickListener(this);
menu.add("item1").setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item){
if(itemName.equals("item0))
{
}
else if (itemName.equals("item1"))
{
}
}

RegisterForContextMenu ImageView?

I'm trying to use a floating context menu and I wonder if it's possible to activate this menu, by pressing the image in the ImageView?
My first problem is how to handle registerForContextMenu and the ImageView? I searched and find most examples with GridView and ListViews.
I have made the menu in xml and I should I use this method in the activity with a switch:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
Just like the others, you get your View in onCreateContextMenu, based on that you inflate the menu for the proper item.
registerForContextMenu(imageView);
The method above expects any View class.
Each time you call registerForContextMenu() for a different View, onCreateContextMenu() will be called for you to handle the proper menu creation.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId == R.id.youtImageView) {
getMenuInflater().inflate(R.menu.image_menu, menu);
}
}
Based on the item id you decide for which View the menu was clicked. You must me sure the id's of menu items for different views are not the same.
When the item from a context menu is clicked, you will receive the onContextItemSelected() callback with MenuItem that was clicked
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.image_menu_item_do_something:
doSOmething();
return true;
default:
return super.onContextItemSelected(item);
}
}
Make sure that you have these methods in onCreate:
ImageView image = (ImageView) findViewById(R.id.image_view);
registerForContextMenu(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(image);
}
});
And in context_menu_main.xml, that it looks similar to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_option"
android:title="#string/action_option_text" />
</menu>
Finally, you'll need to override these two methods as follows:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu_main, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_option:
Log.e("TAG", "Option");
return true;
default:
return super.onContextItemSelected(item);
}
}

Invoke android context menu on menu item press

Can anyone kindly guide as to how can I invoke a context menu on the press of a menu item. I googled a lot for the same, but nothing turned up.
Look forward for your valuable help.
Regards,
Rony
You are probably looking for openContextMenu(view). Call it in your Menu's onclick()
To create a context menu, override onCreateContextMenu and onContextItemSelected. Refer google for examples.
You only need to implement this function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}

Categories

Resources