Align Toolbar icons from right to left?
Asked Answered
G

3

14

In my application, I have implemented some languages including Arabic language which starts from Right to Left. I want to align all icons in the Toolbar be RIGHT_TO_LEFT.

I tried layout_gravity="right" and gravity="right" But nothing happed.

My Toolbar code:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary_color"
    app:popupTheme="@style/Theme.AppCompat.Light"/>

Here is what I want:


Gainful answered 28/3, 2015 at 12:23 Comment(0)
T
23

Official right-to-left support was introduced with Android 4.2 (API level 17), so this will only work if your minimum SDK level is 17:

1) Put the android:supportsRtl="true" attribute in your AndroidManifest file to support right-to-Left layout directions.

2) Secondly put this code in your activity's onCreate() methdod:

if (getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR){
            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

This will make your everything is placed from right to left if the system uses an according language. Also make sure you use start/end instead of left/right when setting up padding to make sure that all elements are aligned correctly.

For more information about the official support announcement: http://android-developers.blogspot.be/2013/03/native-rtl-support-in-android-42.html

Terr answered 28/3, 2015 at 13:46 Comment(2)
Thanks, that worked. However the required API limited me for not use it because many devices still have lower API's. also Upvoted ;)Gainful
for toolbar this works only if I restart the app. Have been using the same for other view and it does work without app restart. .Fimble
U
6

Put the android:supportsRtl="true" attribute in your AndroidManifest file. After that add below line to your toolbar tag:

android:layoutDirection="rtl"
Unmeriting answered 26/8, 2019 at 10:23 Comment(0)
L
1

For those who are looking to have only the hamburger icon to right and have only the icon in toolbar, you can use

android:layoutDirection="rtl"

on the toolbar tag

Lexicologist answered 18/3, 2022 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.