5

I need to put a menu item icon at right of text (by default it's on the left side) so how to change menu item gravity to right in NavigationView ?

because we can not custom NavigationView Items,I think it's impossible. can any one help me.

my menu xml file is:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/menu_group_2">
        <item
            android:id="@+id/menu_settings"
            android:title="Settings" />
        <item
            android:id="@+id/menu_about"
            android:title="About" />
    </group>
</menu>

and my navigationview in layout is

<android.support.design.widget.NavigationView
    android:id="@+id/vNavigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#ffffff"
    app:headerLayout="@layout/view_global_menu_header"
    app:itemIconTint="#8b8b8b"
    app:itemTextColor="#666666"
    android:gravity="right"
    app:menu="@menu/drawer_menu" />
ali
  • 411
  • 5
  • 15

3 Answers3

1

try this: change Navigation layout gravity to:

android:layout_gravity="start"
hassan
  • 29
  • 1
  • 6
  • 1
    I need the menu icon on the right of text. this change only Navigation View gravity. – ali Sep 16 '15 at 16:37
0

add to your Activity before setContentView(R.layout.YOUR_LAYOUT);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
Ahmad Vatani
  • 1,630
  • 4
  • 21
  • 34
0

You can use app:actionLayout attribute to customize the item the way you want.
Here is an example:
menu_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/nav_favorite"
        android:title=""
        app:actionLayout="@layout/action_layout_favorite" />

    <!--Some more items-->

</menu>

action_layout_favorite.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center|end"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:src="@drawable/ic_heart_solid"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="@string/favorite"
        android:textColor="@color/Black"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintEnd_toStartOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

here is the result shown at the first itementer image description here

I got this solution from this website: https://stablekernel.com/using-custom-views-as-menu-items/

Mohamed Medhat
  • 761
  • 11
  • 16