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?