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"))
{
}
}
Related
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);
}
}
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);
}
}
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);
}
}
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
The questions in the title, had a look around but couldnt find any answers,
thanks
Try setting a boolean in
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
and then unsetting it in
#Override
public void onContextMenuClosed(Menu menu) {
super.onContextMenuClosed(menu);
}