Ordering activity bar items not working - android

I have 4 items defined in xml as secondary then later I create one more in the code defined as container. I'm trying to get the one created in code (share) to be first, it's not working. It is always added to the end of the menu. I was under the impression that the menu category is what decided the action bar order. What am I missing?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_unlock"
android:menuCategory="secondary"
android:title="Unlock"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_share"
android:menuCategory="secondary"
android:title="Share App"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_follow"
android:menuCategory="secondary"
android:title="Follow"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_review"
android:menuCategory="secondary"
android:title="Review"
android:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
actions = menu;
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.layout.menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* Set whether share buttons should be available.
*/
public void setShareable (boolean shareable)
{
if (!shareable)
return;
// Build share button
MenuItem item = actions.add (Menu.NONE, Menu.NONE, Menu.CATEGORY_CONTAINER, "Share");
item.setShowAsAction (MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

The correct usage of menu categories is to use FIRST instead of CONTAINER for the menu item that needs to get highest priority, and the rest as SECONDARY as used in the question.
An alternate way to order the menu items is through the android:orderInCategory attribute. Set the first preexisting menu item to, say, order 10 (even 2 would work in your case), and then you can add up to 9 other menu items before it, dynamically, by setting this attribute in the code (menu.add(Menu.NONE,Menu.NONE, order,R.string.my_menu_item). The order variable here is a little tricky, since it's an int that represents both the group association and the order within that group: see here.

Related

How to use two option menus in one toolbar?

I want to use two OptionsMenu in one Toolbar.
First optionMenu from Activity, and items from this menu must be on the end of Toolbar.
Second optionMenu from different Fragment's, (each has own menu) and must be before first menu.
This looks like:
In Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
In res/menu/menu.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="com.nexttek.android.menu.activity.YourActivity">
<item
android:id="#+id/action_one"
android:orderInCategory="100"
android:title="#string/action_one"
app:showAsAction="never" />
<item
android:id="#+id/action_two"
android:icon="#drawable/ic_image"
android:orderInCategory="100"
android:title="#string/action_two"
app:showAsAction="always" />
</menu>
By app:showAsAction, When and how this item should appear as an action item in the app bar. A menu item can appear as an action item only when the activity includes an app bar. Valid values,
always :Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action bar.
collapseActionView :The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
ifRoom :Only place this item in the app bar if there is room for it.
never :Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
withText :Also include the title text (defined by android:title) with the action item.

How to change the order of MenuItems in menu of NavigationView in code?

I have a menu in NavigationView that have 7 menu items, some of these menu items should be invisible based on user settings and the remaining visible items should be displayed in different order. the items are already defined in the XML menu layout.
I've googled a lot but nothing related to already defined menu items. most of the solutions was suggesting to create the menu items in code and set the order while creating it.
here is my menu items XML layout:
<group android:checkableBehavior="single">
<item
android:id="#+id/Home_menuitem"
android:title="Home" />
<item
android:id="#+id/Register_menuitem"
android:title="Register" />
<item
android:id="#+id/Login_menuitem"
android:title="Login" />
<item
android:id="#+id/language_menuitem"
android:title="Language" />
<item
android:id="#+id/ContactUs_menuitem"
android:title="Contact Us" />
<item
android:id="#+id/Likes_menuitem"
android:title="Likes" />
<item
android:id="#+id/Subscription_menuitem"
android:title="Subscription" />
<item
android:id="#+id/Logout_menuitem"
android:title="Logout" />
</group>
say I want to change the order in Code (NOT in XML) to make "Likes_menuitem" to be displayed above "Home_menuitem"
I didn't try it but you probably can follow these steps.
Get a reference to the menu first
navigationView.getMenu()
Add or remove specific items to (from) it with different order to achieve your order.
add(int groupId, int itemId, int order, CharSequence title)
removeItem(int id)
More information in the docs!
If you add items programmatically just change the order of adding items. Even if you change the position of menu inflating it will affect the result order.
menu.add(Menu.NONE, 898, Menu.NONE, "Item title") // <- Will be 1st
inflater.inflate(R.menu.menu_progress_avatar, menu) // <- Will be 2nd
You can do this by visible menuitem to true or false.

Removing the settings menu inflator from Actionbar

I have a action bar set up as this:
When I click on it a settings popup appears like this which when pressed takes me to my apps settings page.
My question is how would I remove the settings popup so that it takes me directly to my settings activity when I press the three dotted button? I tried playing around with code below but it yielded no result.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Goto the menu folder of your project. res>>menu and find the xml file representing the
You have to add the android:showAsAction="always" like below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
Use the showAsAction parameter.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
You can specify that your settings option always appears as a button on the action bar. You don't need the three dots in this case.
For instance, you can specify your action bar menu item like this (notice the "always" value):
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always" />

ActionBar item does not shown in Option Menu

I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.

Option menu same as like Whatsapp

I want Option menu in Action Bar same as like Whatsapp and many other applications.
Check Image shown as below :
Please help me to get this.
Android documentation says to you how to do that, here.
You need to create a new xml inside menu res folder.
An example:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
android:icon: Used to specify the icon to use
android:title: The name of the action (Example, if the button does a Search it could be "Search")
android:id: The ID of the menu, you will use it to know which menu has been clicked
Then to create it in your Activity override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Replace R.menu.main_activity_actions with your menu name.
To apply the same effect like in yuor screen you should use android:showAsAction="never"
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="never"
/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose"
android:showAsAction="never" />
</menu>
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
android:showAsAction is used to say how Android should show the icon; if you put ifRoom Android will try to add it as icon if there are space; if you use "never" it will add it in the overflow menu.
Official doc:
ifRoom Only place this item in the Action Bar if
there is room for it.
withText Also include the title text (defined by
android:title) with the action item. You can include this value along
with one of the others as a flag set, by separating them with a pipe
|.
never Never place this item in the Action Bar.
always Always place
this item in the Action Bar. Avoid using this unless it's critical
that the item always appear in the action bar. Setting multiple items
to always appear as action items can result in them overlapping with
other UI in the action bar.
collapseActionView The action view
associated with this action item (as declared by android:actionLayout
or android:actionViewClass) is collapsible.
Introduced in API Level 14.
If you use it in API < 14 it will just be ignored.

Categories

Resources