How can I add menu items to the menu of the DrawerLayout in Java and set onOptionsItemSelected to it. (I don't know how many items are needed; the user adds them)
I have an arraylist of custom objects that have a title that I want to display in the menu and when clicked on the item I want to change what is being displayed in the main Activity, by passing which one of the objects was clicked on.
I know that it is possible to add a menu item like this, but the problem is that I don't know how many there are going to be, so setting the onOptionsItemSelected to the right amount of items is what I am looking for
private static final int MENU_ITEM_ITEM1 = 1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
return true;
default:
return false;
}
}
Thanks for your help!
EDIT:
I see how how this might seem like a duplicate question, but the other questions and answers did not help. I am looking for a way to set onOptionsItemSelected to an unknown amount of items (the size of an ArrayList) and then display the selected Object in the main Activity
Thanks
Related
At the beginning I have two items and the 2nd gets a listener with:
final View view = findViewById(R.id.list_item);
if (view != null) {
view.setOnLongClickListener(v -> {
Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
return true;
});
}
}
But if I add a new item with menu.add("name"); the listener is on the newly added item and not on the old one (list_item)
I have already tested many other alternatives, but they don't work!
I just need help on how to keep the item ID or how the listener stays on the item.
When you add an item using ``menu.add(...)` it returns a reference to the newly added item. You can set the listener like this:
final MenuItem newItem = menu.add("New item");
newItem.setOnMenuItemClickListener(menuItem -> {
// Handle your click here!
return true;
});
However, you should add/remove menu items by overriding onPrepareOptionsMenu(Menu menu). When you use menu.add(...) use the overload that allows you to set the itemId:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 314159, Menu.NONE, "New item");
return super.onPrepareOptionsMenu(menu);
}
Note that the above will be called each time the menu is going to be shown (very convenient).
Then you can handle the menu being selected from onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 314159) {
// Handle menu selection here!
}
return super.onOptionsItemSelected(item);
}
Check the AppBar/Menu guide here: https://developer.android.com/training/appbar
i need Help, i'm working on a Simple Text Editor and i want to implement a custom Menu Copy Past Cut .. and more, but to do that i need to Hide the Original Menu, My problem is just in this Step of Hiding the original Menu (Copy.. Past..) it Kepp showing while using the common ways, such as the first bellow Code.
this way not working at all.
and also the way of disabling the long click (if disabling the Long click we can't select the Text) and if text not selected can't use my custom menu.
in some Application such as Code Editor, have this function :
so, which way i follow to implement this ? can you guide/help me plz ! bcz now all steps is ok for me (when selecting text my menu appear) But also the original Menu (copy past cut) appear and i want to disable it...
the Code Bellow not working and the screen shot show that
edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
finally i got the solution with help of guide in web, and with my own search, no body wanted to share the solution with me.
after i got it, i want to share with you. maybe it helps someone.
the suggestions shared all not working, some will disable selecting texts and others also do this + not working on all api..
in my case the editText id is edittext1.
in your activity past the bellow code :
private class MyActionModeCallback implements ActionMode.Callback {
private Menu mMenu;
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
//After the menu is created, the object is obtained for subsequent operations
this.mMenu=menu;
mMenu.clear();
return true;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()){
}
return true;
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
}
and how to use it :
edittext1.setCustomSelectionActionModeCallback(new MyActionModeCallback());
Heyy,
I have implemented menu, where there are two menu items and when I click on first item, the other menu item goes disabled and vise-versa.
And when I click on back button navigation icon, I have to check if first item is disabled or not and if disabled, then turn the second item enabled, and if not then onBackPressed();
So, I don't know how to recognize which item is disabled.
Please help me fast.
There are some code references
This is my current try and it also have some errors. Please help me find another way or fix this code.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (menu.getItem(0).getActionView().getVisibility() == View.VISIBLE) {
onBackPressed();
} else {
menu.getItem(1).setEnabled(false);
menu.getItem(1).setVisible(false);
menu.getItem(0).setEnabled(true);
menu.getItem(0).setVisible(true);
}
}
});
in onCreateOptionsMenu() store the menu into a local class field, and then to check if a certain menu item is enabled/disabled use isEnabled()
MenuItem item = menu.findItem(R.id.item_id);
if (item.isEnabled()) {
// enabled
} else {
// not enabled
}
Or you can use the order of the item among the menu items to get a particular item
MenuItem item = menu.getItem(0);
I need to change my menu icon based on a condition, however when I use Menu.getItem(index).setIcon() it doesn't change my icon at all. I need help how to do that please
Menu.getItem(index).setIcon()
if (menu != null) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
Your code looks correct, but maybe you are using it just once inside onCreateOptionsMenu() and missing the calls that are necessary to update the icon as observedCount updates.
Approach 1
You can try doing the following in your activity:
First, override onPrepareOptionsMenu() and apply the changes there:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
return super.onPrepareOptionsMenu(menu);
}
Then, whenever you want the icon to be updated (i.e., whenever observedCount changes), just invoke:
invalidateOptionsMenu(); // From a fragment, call activity.invalidateOptionsMenu();
Approach 2
Another option is to save a reference to the menu inside onCreateOptionsMenu():
private Menu mMenu;
...
#Override
public void onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenu = menu;
}
Then, you can use that reference to set the icon:
if (observedCount != 0)
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
That should hopefully do it!
P.S. Make sure you are getting the correct item when doing menu.getItem(2) (the item number 2 is actually the third one because the count starts at 0). To avoid getting the wrong item, it would be a good idea to set the item you want to change with an ID and then replace menu.getItem(2) with menu.findItem(R.id.your_item_id).
When I touch this "Edit" button I want it to change to "Done". Is there a way to do this?
I tried this but it only works when opening the menu on the right side:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// change the text here.
return super.onPrepareOptionsMenu(menu);
}
I assume that, at some point, you inflate the menu, yes?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.some_menu_layout, menu);
return true;
}
So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)