3

I want to customize android.support.design.widget.NavigationView class to make NavigationItems right to left such that navigation item icons appear at the right side of the text.

Support library versions used:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'

This is code used for activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    android:id="@+id/app_bar_main"
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.myapp.support.CustomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimaryDark"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemIconTint="@color/drawer_item_tint"
    app:itemTextColor="@color/drawer_item_text"
    app:menu="@menu/activity_main_drawer">

</com.myapp.support.CustomNavigationView>


</android.support.v4.widget.DrawerLayout>

This is my CustomNavigationView class inherited from NavigationView class in the support library:

package com.myapp.support;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.NavigationView;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;

public class CustomNavigationView extends NavigationView{
    public CustomNavigationView(Context context) {
        this(context, null);
    }

    public CustomNavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //for api levels more than 17
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            ViewCompat.setLayoutDirection(this, ViewCompat.LAYOUT_DIRECTION_RTL);

    }

}

I used ViewCompat.setLayoutDirection method; but it works only for API level 17 and more. I did not found any other methods or attributes like LayoutParams. So what should I do for lower levels? Is there any other ways to force icons to appear at the right side of the text?

Forough
  • 262
  • 4
  • 20

1 Answers1

0

I think this should work here are the things you can do programtically and alo in xml

1> android:layout_gravity="right" or TO end

Tip try setting this property in android.support.v4.widget.DrawerLayout tag and also in com.myapp.support.CustomNavigationView tag.

secondly in code :

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, R.string.drawer_open,
        R.string.drawer_close) {
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item != null && item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                mDrawerLayout.closeDrawer(Gravity.RIGHT);
            } else {
                mDrawerLayout.openDrawer(Gravity.RIGHT);
            }
        }
        return false;
    }
};
Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • It is not usefull too. I think it only forces drawer to open from right side of the page. I need that the icons in the navigation menu items place at the right side of the coresponding text. – Forough Dec 04 '15 at 18:16
  • Thne you have posted the question in wrong style and context – Coas Mckey Dec 07 '15 at 08:28
  • Maybe question title is not complete but I explained what I exactly need in the first lines : "to make NavigationItems right to left such that navigation item icons appear at the right side of the text. " – Forough Dec 08 '15 at 12:26