Diberdayakan oleh Blogger.

Selasa, 24 Juni 2014

Set items ActionBar Position

by Joielechong sipayung  |  in pemrograman visual at  Selasa, Juni 24, 2014

To positioning menu items of ActionBar, we can use android:menuCategory="system" and android:orderInCategory tag. the lower value of android:orderInCategory tag will be placed on the left. OrderInCategory will determines the order appearance, from low to high.

From my case, before I use these tag, with the following code in my res/menu/main.xml:

<menu 
...
<item
        android:id="@+id/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="1"
        app:showAsAction="collapseActionView|ifRoom"
        android:title="@string/action_search_unit"/>
    <item
        android:id="@+id/menu_share"
        android:icon="@drawable/ic_action_share"
        android:title="Share"
        android:orderInCategory="10"
        app:showAsAction="always"/>
    
    <item
        android:id="@+id/action_help"
        android:icon="@drawable/ic_action_help"
        android:showAsAction="ifRoom"
        android:orderInCategory="50"
        android:title="Help"/>
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

</menu>

The result is like the picture in Picture 1:


Picture 1. Before using android:menuCategory="system"

Eventhough the value of android:OrderInCategory of Search menu item is the lowest of the all menu items, the icon still in the second place from the left.


So to force Search menu item to the left, we must add android:menuCategory="system" to all of menu items in res/menu/main.xml :

<menu 
     ...
    <item
        android:id="@+id/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:menuCategory="system"
        android:orderInCategory="1"
        app:showAsAction="collapseActionView|ifRoom"
        android:title="@string/action_search_unit"/>
    <item
        android:id="@+id/menu_share"
        android:icon="@drawable/ic_action_share"
        android:title="Share"
        android:menuCategory="system"
        android:orderInCategory="10"
        app:showAsAction="always"/>
    
    <item
        android:id="@+id/action_help"
        android:icon="@drawable/ic_action_help"
        android:showAsAction="ifRoom"
        android:orderInCategory="50"
        android:menuCategory="system"
        android:title="Help"/>
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:menuCategory="system"
        app:showAsAction="never"/>

</menu>

Now, we get the Search menu item on the left like the picture in Picture 2:


Picture 2. After using android:menuCategory="system"


Happy Coding.. :D ;).



0 comments:

Silahkan tinggalkan komentar anda: