Priority icon on the action bar (storage order of items)

I am trying to control which elements of the action bar are displayed if there is limited space, and have tried the solution in this question and answer: Priority icon in the action bar

Everything works fine, except that I want to keep the order of the icons in a way that differs from the priority, while the solution above makes the elements reorder. Is there a way to set the priority for displaying items without changing the display order of items in the action bar?

I tried to change the order of elements in xml, and this does not affect the order of elements as displayed (without orderInCategory, changing the order in the file leads to a change in the order of elements as shown).

I am developing against ICS.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_edit" android:icon="@android:drawable/ic_menu_edit" android:title="@string/action_edit" android:showAsAction="ifRoom" android:orderInCategory="1" /> <item android:id="@+id/action_undo" android:icon="@android:drawable/ic_menu_revert" android:title="@string/action_undo" android:showAsAction="ifRoom" android:orderInCategory="2" /> <item android:id="@+id/action_delete" android:icon="@android:drawable/ic_menu_delete" android:title="@string/action_delete" android:showAsAction="ifRoom" android:orderInCategory="3" /> <!-- I want this item to be on the right, but shown in preference to any other items; setting it orderInCategory to 0 ensures that it is shown in preference to other items, but always places it on the left --> <item android:id="@+id/action_addnew" android:icon="@android:drawable/ic_menu_add" android:title="@string/action_addnew" android:showAsAction="ifRoom" android:orderInCategory="0" /> </menu> 

Thanks.

+2
source share
4 answers

Elements are placed from right to left , so by rearranging the elements, it can help

 <item android:id="@+id/action_addnew" android:icon="@android:drawable/ic_menu_add" android:title="@string/action_addnew" android:showAsAction="always" android:orderInCategory="0" /> <menu> <item android:id="@+id/action_edit" android:icon="@android:drawable/ic_menu_edit" android:title="@string/action_edit" android:showAsAction="ifRoom" android:orderInCategory="1" /> <item android:id="@+id/action_undo" android:icon="@android:drawable/ic_menu_revert" android:title="@string/action_undo" android:showAsAction="ifRoom" android:orderInCategory="2" /> <item android:id="@+id/action_delete" android:icon="@android:drawable/ic_menu_delete" android:title="@string/action_delete" android:showAsAction="ifRoom" android:orderInCategory="3" /> </menu> 
+2
source
 android:orderInCategory 

This feed works from right to left, so you need to make the correct code in accordance with this.

0
source

What I got from reading your question is that you want to display action_addnew should appear on the right, and it should always appear to everyone.
Try this option

  <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_edit" android:icon="@android:drawable/ic_menu_edit" android:title="@string/action_edit" android:showAsAction="ifRoom" /> <item android:id="@+id/action_undo" android:icon="@android:drawable/ic_menu_revert" android:title="@string/action_undo" android:showAsAction="ifRoom" /> <item android:id="@+id/action_delete" android:icon="@android:drawable/ic_menu_delete" android:title="@string/action_delete" android:showAsAction="ifRoom" /> <item android:id="@+id/action_addnew" android:icon="@android:drawable/ic_menu_add" android:title="@string/action_addnew" android:showAsAction="always" /> </menu> 
0
source

From left to right, indicate the first element

 android:orderInCategory="0" 

and second element

 android:orderInCategory="1" 

etc.

0
source

Source: https://habr.com/ru/post/927852/


All Articles