Is it possible to have 2 context menus in same activity? - android

I have an activity with TextView and an image icon. I already created context menu for the image icon by overriding onCreateContextMenu().
However, I would like also to have another context menu for the TextView. However, I notice I'm already utilizing onCreateContextMenu():
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
}
Hence, is there away to add another context menu for the TextView?

you can use the passed View object in onCreateContextMenu to determine the owner of the menu and populate a menu accordingly.
Your code should look something like this :
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.imageIconId:
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
break;
case R.id.textViewid:
// do whatever you want with the menu object.
break;
}
}

The parameters passed to you by that method will help you branch to different things.
Here is an example:
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if( v.equals(yourImgView) ){
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
}else if ( v.equals(yourTxtView) ) {
//Do your textView things.
}
}

Related

Hiding context menu items

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

Android Context Menu for any View

As far as I knew Context Menu can be registered for any view not only for ListView. Today I am trying to register context menu for an Custom ImageView. Like this:
// In onCreateView() method of activity
registerForContextMenu(mViewHolder.profileImageView);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case CONTEXT_MENU_ITEM_CHANGE_PICTURE:
//Toast.makeText(mParentActivity, "Delete " + info.position + "th item", Toast.LENGTH_LONG).show()
Toast.makeText(this, "Go to library", Toast.LENGTH_SHORT).show();
return true;
default:
return true;
}
}
I didn't find any example of registering context menu without ListView. Would anyone give me one example or can figure out what I am missing ?
you need to write super.onCreateContextMenu(menu, v, menuInfo);
after all code like:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

Add delete function in my project

I use this code for my contextual menu and I make a simple commands. But how to integrate real commands in this?
I want make real function.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Optiuni");
menu.add(0, v.getId(), 0, "Copiaza");
menu.add(0, v.getId(), 0, "Sterge");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Copiaza"){copyFunction(item.getItemId());}
else if(item.getTitle()=="Sterge"){deleteFunction(item.getItemId());}
else {return false;}
return true;
}
public void deleteFunction(int id){
Toast.makeText(this, "Sters", Toast.LENGTH_SHORT).show();
}
public void copyFunction(int id){
Toast.makeText(this, "Copiat", Toast.LENGTH_SHORT).show();
}

Context Menu not being displayed

Here is the section of my code in onCreate() Method:
LayoutX = (LinearLayout) findViewById(R.id.LL_SomeName);
LayoutX.setClickable(true);
registerForContextMenu(LayoutX);
Here is rest of the code
CreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu M, View V, ContextMenuInfo CMI) {
super.onCreateContextMenu(M, V, CMI);
M.setHeaderTitle("My Title Here");
M.add(0, V.getId(), 0, "Menu 1");
M.add(0, V.getId(), 0, "Menu 2");
}
ItemSelected
#Override
public boolean onContextItemSelected(MenuItem Item) {
Toast.makeText(getApplicationContext(), Item.getTitle(), Toast.LENGTH_LONG).show();
}
The Context Menu does not appear at all. Am I missing anything ?
this may help you
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select one");
menu.add(0, 1, 0, "Edit");
menu.add(0, 2, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
// edit option is selected
}

How can I tell which button has accessed the context menu

In my soundboard app I have created a context menu using this code.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose an option");
menu.add(0, v.getId(), 0, "Save as ringtone");
menu.add(0, v.getId(), 0, "Save as Notification");
menu.add(0, v.getId(), 0, "Save as Alarm");
menu.add(0, v.getId(), 0, "Exit Menu");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
// Global.currentsound = info.id;
if(item.getTitle()=="Save as ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save as Notification"){function2(item.getItemId());}
else if(item.getTitle()=="Save as Alarm"){function3(item.getItemId());}
else {return false;}
return true;
}
and called on the buttons like this
Button cmenu = (Button)findViewById(R.id.s1sound1);
registerForContextMenu(cmenu);
Now I want to pass the information for each button to the function in the code to set the sound according to which button was pressed. How would I do this without creating a separate context menu for each button which would be madness.
Thanks
I think it is stored in the "View v", Button extends View, so you have to cast it. I'm not sure, but you can check by setting a breakpoint on the
super.onCreateContextMenu(menu, v, menuInfo);
line, and checking the debugger.

Categories

Resources