I have a Toolbar (android.support.v7.widget.Toolbar) and one Action Menu Item on it. I want to programmatically populate submenu for the menu item.
I could not find a way to do it until I set the toolbar as ActionBar by setSupportActionBar(toolbar). In this case I can modify menu items in onPrepareOptionsMenu(). Is there a way to change menu items programmatically without setSupportActionBar(toolbar)?
The only action item is inflated by toolbar.inflateMenu(R.menu.menu_main)
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="1"
app:showAsAction="always">
</item>
</menu>
You can get the menu of the Toolbar with the Toolbar's getMenu() method. This method returns a Menu object to which you can add or modify menu items, or submenus.
Here you can check out the documentation of the getMenu() method.
Related
about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>
I'm developing a material design app & I want this effect when I click on the menu option or the three dots on the side of ActionBar/Toolbar.
The effect I want is at the last of 'usage' section & just above the 'menu items' section. It is labeled as 'App bar dropdown'.
The 'three dots' is commonly called the 'overflow' menu on Android, and knowing the terminology will improve your search results.
CodePath has an example on using the Toolbar as ActionBar. It will be helpful to read the whole page, because it explains the history of both Toolbar and ActionBar. The section Using toolbar as ActionBar is particularly relevant to your question.
In your MainActivity onCreate method:
// Set a ToolBar to replace the ActionBar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Your MainActivity will override the onCreateOptionsMenu and inflate your menu resource file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
And you will define a menu resource file:
/res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
The 'showAsAction' attribute tells Android to only display this menu item in the overflow menu (never as an action on the toolbar).
Im working on action menu item and its over flow item this is my main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
android:icon="#drawable/search"
android:title="#string/start_new_project"
app:showAsAction="always" />
<item
android:id="#+id/menu_dts_overflow"
android:icon="#drawable/ic_action_overflow_round"
android:orderInCategory="11111"
android:title="Service"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_newProject"
android:icon="#drawable/newproject"
android:title="#string/start_new_project"
app:showAsAction="never" />
<item
android:id="#+id/menu_help"
android:icon="#drawable/help"
android:title="Service Tasks"
app:showAsAction="never" />
<item
android:id="#+id/menu_signOut"
android:icon="#drawable/signout"
android:title="#string/menusignout"
app:showAsAction="never" />
</menu>
</item>
I tried to construct a search item and a overflow item which you can see in the above code. I'm new to Action bar menu items so i tried to Google it and was able to make it work as I need.
In this I have to know one more thing.
1. What is orderInCategory with some numbers and what for it is used..?
android:orderInCategory is an integer attribute that dictates the order in which the menu items will appear within the menu when it is displayed.
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_first"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/string_one"/>
<item
android:id="#+id/menu_item_second"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/string_two"/>
</menu>
Menu items in ToolBar are arranged from left to right (or
start to end in RTL mode) in the ascending order (i.e. 1,2,3 ->
left to right).
Menu Items in Overflow menu are arranged from top to bottom in
the ascending order (i.e. 1,2,3 -> top to bottom).
android:orderInCategory is actually useful in two ways.
1. For menu items in ActionBar.
Items will appear from left to right in ActionBar depending on the ascending order.
2. For menu items in overflow menu.
Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.
android:orderInCategory Higher value, lower priority.
I have a Activity and a Fragment in it, both of them have option menu, and the item numbers are 1 and 3.
If I set android:orderInCategory=0, the activity menu are above the fragment menu, same effect before I set the value.
But if I set android:orderInCategory=1,the activity menu are below the fragment menu, and that is what I want.(I also test android:orderInCategory=5 tested too, still the same effect.)
Is it possible to add Action Item in ActionBar, which show drop down menu on click?
paint example:
P.S. ActionBar already contains navigation drawer toggle button, title and overflow menu.
Where do I initialize that button, in which xml?
How do I set such drop down operations to Action Item?
How to set the content of such drop down menu?
And how to get access to specific item click action?
Some working code examples would be great!
Thanks in advance. Appreciate any help.
So I found solution by myself.
You need to inflatean Action Item in onCreateOptionsMenu(Menu menu):
getMenuInflater().inflate(R.menu.global_filters, menu);
global_filters.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".NavigationActivity">
<item android:id="#+id/action_filters"
android:title="Фильтры"
android:icon="#drawable/ic_filter_white"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
...which is that downside arrow:
Then create a PopupMenu. I did it in onOptionsItemSelected:
View menuItemView = findViewById(R.id.action_filters); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup_filters_user);
popupMenu.show();
and here you set .xml file with items of drop down menu in file popup_filters_user.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/filter_bought_user"
android:title="Купленые"/>
<item
android:id="#+id/filter_price_user"
android:title="Цена"/>
<item
android:id="#+id/filter_author_user"
android:title="Автор"/>
</menu>
and hooray! Here's the result:
If your menu is in a Fragment then you might use the following to get the View
View menuItemView = MainActivity.getInstance().getWindow().findViewById(R.id.action_filters);
I have an app with a NoActionBar theme. In my main activity I have an options menu that I created manually on the top of the screen (or by utilizing the built in device's options button).
In this main activity, I have a fragment with a listView where I apply the action mode long click functionality, to show the contextual action bar (CAB) for further user options.
Now, I try adding an options item to my CAB so it will contain some options like selecting all items in the listView, but since it's an item of the CAB, I can't really show the popup menu like in a regular activity. Further more, I want all the options menu callbacks (such as onOptionsItemSelected) to stay in the context of the CAB, in order to be able to continue to perform actions on the CAB.
Here's the code of my CAB:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_overflow"
app:showAsAction="always"
android:orderInCategory="200"
android:icon="#drawable/ic_action_overflow"
android:title="Options"
android:visible="false"/>
</menu>
Apparently I missed the built in feature of the CAB - a built in overflow menu that collapses some of the actions' items once the screen is too small to show them all.
Another manipulation that needs to be done in order to always collapse certain actions under that overflow menu is to set for each one of them:
android:showAsAction="never"
app:showAsAction="never"
So, say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be collapsed always under the built in overflow menu, we'll set this in the CAB's xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
mm:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_select"
android:orderInCategory="200"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/select_all"/>
<item
android:id="#+id/action_add"
android:orderInCategory="300"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/button_add"/>