Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Item postition in ActionBar

Tags:

android

I have this layout that add 1 or more items into the ActionBar

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
           android:id="@+id/item_test"                
           android:showAsAction="always|withText"
           android:title="SearchIcon"/> 
</menu>

And then I will add another item dynamically into the ActionBar from another Fragment or Activity

MenuItem populateItem = menu.add("My New Item - Reload Icon");

But I want the item that I just added to stay in front of the items I had before, like the picture:

enter image description here

If I add it like I did, the icons will be shown as the last picture, how can I get the alignment like in the second picture?

like image 459
feco Avatar asked Jul 24 '12 07:07

feco


People also ask

How do you set menu items to the left of the ActionBar?

You will have to create a custom view for that. You can't use the options menu inflation for your use case. Set the style of the activity to Theme. AppCompat and don't use NoActionBar in the activity or the application style.

What is windowActionBar in Android?

android:windowActionBar denotes property for lollipop and above only. Where as windowActionBar denotes for all versions and is retrieved from support library. Follow this answer to receive notifications.

What is action bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


1 Answers

You can re-order the menu items with the android:orderInCategory attribute in each you need to move. You have to play around with that option alogn with menuCategory.

For example, assign to all items:

android:menuCategory="system"

Then, just set lower orderInCategory values to the Items you want to keep on the left.

Order in category determines the order appearance, from low to high.

like image 191
Iñigo Avatar answered Nov 04 '22 18:11

Iñigo