How To Change The Icons Direction For Menu Items In A Navigationview?
Hi I am using android NavigationView. I want to change the icons direction to RTL instead of LTR like this: How can I do this?
Solution 1:
you can use listview inside the navigationview like what i did. the benefit of this is that you can have better control on this:
<android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"android:fitsSystemWindows="true"app:headerLayout="@layout/header"
><ListViewandroid:id="@+id/navigationmenu"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="192dp"android:background="@android:color/white"></ListView></android.support.design.widget.NavigationView>
and nav item should be like this:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/cardview_dark_background"
><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_alignParentRight="true"android:layout_height="wrap_content"android:src="@drawable/viewlist"
/><TextViewandroid:id="@+id/tvlstName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:layout_marginRight="30dp"android:textColor="@color/colorPrimary"
/></RelativeLayout>
and navcustomadapter like this:
package com.example.android.dezcook;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Android on 6/4/2016.
*/publicclassnavCustomAdapterextendsBaseAdapter {
String[] result;
Context context;
int[] imageId;
privatestatic LayoutInflater inflater=null;
publicnavCustomAdapter(MainActivity mainActivity,String[] prgmNameList,int[] prgImages)
{
result=prgmNameList;
imageId=prgImages;
context =mainActivity;
inflater =(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@OverridepublicintgetCount()
{
return result.length;
}
@Overridepublic Object getItem(int position)
{
return position;
}
@OverridepubliclonggetItemId(int position)
{
return position;
}
publicclassHolder
{
TextView tv;
ImageView img;
}
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent)
{
Holder holder=newHolder();
View rowView;
rowView=inflater.inflate(R.layout.navitem,null);
holder.tv=(TextView)rowView.findViewById(R.id.tvlstName);
holder.img=(ImageView)rowView.findViewById(R.id.img);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Toast.makeText(context,"you Clicked "+result[position],Toast.LENGTH_LONG).show();
}
}
);
return rowView;
}
}
finally call it in main activity like this:
ListView lv=(ListView)findViewById(R.id.navigationmenu);
lv.setAdapter(newnavCustomAdapter(this,navItem,prgmImages)
Solution 2:
Just figure it out!
Try adding these attributes to NavigationView
in your layout file.
android:layoutDirection="rtl"
android:textDirection="rtl"
Screenshot
Note: It seems its working on API 17 and higher
Solution 3:
I had this problem.My app was "ltr" and I wanted to view drawer in "rtf" mode. My solution: In Navigation view I cleared :
- app:menu="@menu/drawer_menu"
- app:headerLayout="@layout/drawer_nav_header"
and replaced:
- include layout="@layout/drawer_nav_header"/>
include layout="@layout/ly_custom_menu_item"/>
<includelayout="@layout/fragment_map" /><FrameLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent" /><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="right"android:background="@color/white"android:fitsSystemWindows="true"><!--app:menu="@menu/drawer_menu" app:headerLayout="@layout/drawer_nav_header"--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><includelayout="@layout/drawer_nav_header"/><includelayout="@layout/ly_custom_menu_item"/></LinearLayout></android.support.design.widget.NavigationView>
ly_custom_menu_item.xml:
<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns: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:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/linearLayout"android:background="@color/white"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:id="@+id/linearItemAnalysis"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="@dimen/picSize_50"android:orientation="horizontal"android:gravity="center_vertical|right"android:paddingRight="@dimen/margin_dp_8"android:foreground="?selectableItemBackgroundBorderless"><TextViewandroid:text="navAnalysis"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="@dimen/margin_dp_16"/><android.support.v7.widget.AppCompatImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ic_analysis_menu"app:layout_constraintEnd_toEndOf="parent"/></LinearLayout><LinearLayoutandroid:id="@+id/linearItemRouting"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="@dimen/picSize_50"android:orientation="horizontal"android:gravity="center_vertical|right"android:paddingRight="@dimen/margin_dp_8"android:foreground="?selectableItemBackgroundBorderless"><TextViewandroid:text="routing"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="@dimen/margin_dp_16"/><android.support.v7.widget.AppCompatImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ic_routing"app:layout_constraintEnd_toEndOf="parent"/></LinearLayout><LinearLayoutandroid:id="@+id/linearItemAddress"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="@dimen/picSize_50"android:orientation="horizontal"android:gravity="center_vertical|right"android:paddingRight="@dimen/margin_dp_8"android:foreground="?selectableItemBackgroundBorderless"><TextViewandroid:text="navAddress"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="@dimen/margin_dp_16"/><android.support.v7.widget.AppCompatImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ic_address"app:layout_constraintEnd_toEndOf="parent"/></LinearLayout></LinearLayout></ScrollView>
Solution 4:
Try to set your navigation drawer item layout gravity. Example :
android:gravity="right"
Post a Comment for "How To Change The Icons Direction For Menu Items In A Navigationview?"