Search code examples
androidandroid-layoutandroid-toolbar

Android Toolbar move icon of MenuItem to right side


I have seen it done in apps like QuickPic where they managed to put the icon on right side

enter image description here

I tried to make the same thing but then I realized the icon seems to be hard-coded to appear on the left side.

enter image description here

Below is my menu.xml

<menu>
    <item android:id="@+id/sort_by_name"
        android:title="By name"
        android:icon="@drawable/ic_arrow_downward_gray_24dp"
        />

    <item android:id="@+id/sort_by_date"
        android:title="By date"/>

    <item android:id="@+id/sort_by_path"
        android:title="By path"/>
</menu>

So is there any way to move the icon to the right side?


Solution

  • you can always use android:actionLayout and create your own View that looks the way you want.

    Example:

    your_custum_view:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvMenu"
        android:clickable="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:drawableRightCompat="@drawable/ic_up_arrow"
        android:drawablePadding="@dimen/margin_medium"
        android:focusable="true"
        android:gravity="center"
        android:padding="6dp"
        android:orientation="vertical"
        android:text="By Name" />
    

    your_menu:

    <item
            android:id="@+id/nv_create"
            android:icon="@drawable/ic_create_dw"
            android:title="Create"
            app:actionLayout="@layout/menu_sign"
            app:showAsAction="always" />