1

I am adding menu items to navigation drawer pragmatically. All the items are added to the left of navigation view but when I add custom menu item, it adds the item to the right of navigation view.

I am adding menu items pragmatically as below:

        Menu menu = mNavigationView.getMenu();
        if (menu == null) return;

        // add home menu
        menu.add(0, 1, Menu.NONE, "Home");

        // add refer menu
        menu.add(0, 2, Menu.NONE, "Refer and Earn");

        // add points menu
        MenuItem menuItem = menu.add(0, 3, Menu.NONE, null);
        menuItem.setActionView(R.layout.layout_nav_menu_points);

        // add settings menu
        menu.add(0, 4, Menu.NONE, "Settings");

        // add about us menu
        menu.add(0, 5, Menu.NONE, "About us");

        // add logout menu
        menu.add(0, 6, Menu.NONE, "Logout");

Below is the code of layout_nav_menu_points:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:gravity="start|center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Points"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_14" />

        <ImageView
            android:layout_width="@dimen/dimen_5"
            android:layout_height="@dimen/dimen_5"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:contentDescription="@string/app_name"
            android:src="@drawable/shape_red_dot" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_marginLeft="@dimen/dimen_2"
        android:layout_marginRight="@dimen/dimen_2"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:text="pt"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />
    </LinearLayout>

</LinearLayout>

Getting below result: enter image description here

EDIT: I tried using RelativeLayout instead of LinearLayout but getting the same result. Below is the code of layout_nav_menu_points using RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:gravity="start|center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Points"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_14" />

        <ImageView
            android:layout_width="@dimen/dimen_5"
            android:layout_height="@dimen/dimen_5"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:contentDescription="@string/app_name"
            android:src="@drawable/shape_red_dot" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_below="@id/ll_points"
        android:layout_marginLeft="@dimen/dimen_2"
        android:layout_marginRight="@dimen/dimen_2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:text="pt"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />
    </LinearLayout>

</RelativeLayout>

EDIT: Below is the code for shape_red_dot

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

        <solid
            android:color="#EC2027" />

        <size
            android:height="10dp"
            android:width="10dp" />
    </shape>

Anyone please suggest me how to move that "point" menu to the left like rest of the menu items. Thanks

Geeky Singh
  • 1,003
  • 9
  • 20

1 Answers1

0

In your nav_menu_points.. Please do change your layout_gravity and gravity value.....

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

    *** CHANGED CODE
    android:layout_gravity="left"
    android:gravity="left|center_horizontal"
    *** CHANGED CODE ENDS

    android:orientation="vertical">

Result Image:

enter image description here

Difference between gravity and layout_gravity.. Well gravity, set your contents gravity (where will it be) whereas, layout_gravity, set your layout's gravity.

Hope it solves the problem. Cheers!

O_o
  • 1,103
  • 11
  • 36
  • You can see in my question that I have already tried this code. – Geeky Singh Oct 05 '16 at 08:00
  • I have changed the gravity and layout_gravity value, marked inside stars.. – O_o Oct 05 '16 at 08:02
  • I used "start" instead of left for gravity and layout_gravity value to support RTL. So it will be same if I use "left" instead of "start" – Geeky Singh Oct 05 '16 at 08:03
  • @GaganSingh, used my image to recreate the scenario in my end. came out that. – O_o Oct 05 '16 at 08:10
  • This is just preview of layout. I am getting the same preview but when I add this layout to navigation menu, it is appearing on the right side not the left side. Try to set this layout to navigation menu item and see the result. – Geeky Singh Oct 05 '16 at 08:12