Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Navigation button show wrong side when layout direction is RTL

I asked this question before (here) but nobody answered so I ask It a little bit simpler. The problem is when I change the layout direction to RTL (in xml file : android:layoutDirection="rtl" or programmatically :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindows().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

doesn't matter) the navigation icon remain LTR

enter image description here

How can I fix this issue?

To be more specific the arrow should point at right!

like image 463
Hamidreza Salehi Avatar asked Aug 12 '15 11:08

Hamidreza Salehi


People also ask

How do I change RTL on android?

Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.

How to enable RTL support in android?

First of all, you must add android:supportsRtl="true" to the <application> element in your manifest file for your app to supporting RTL design. Trick: If your app supports multiple languages and if you have code snippet like config. setLayoutDirection(Locale.US) you must change it.

What is force RTL layout direction?

This layout is for the languages which is written from right to left like Arabic languages. On checking this option you will force the layout and check if the text and other views are properly aligned to the language direction.

At which API version does support for the android right to left RTL API begin?

RTL text mirroring is only supported in apps when used on devices running Android 4.2 (API level 17) or higher.


2 Answers

In the Activity Class --> Add these lines :

onCreate(){
    ActionBar ab = getSupportActionBar();    
    // Enable the Up button    
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp, getApplicationContext().getTheme()));

    }
@Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent login = new Intent(LoginActivity.this, MainActivity.class);
        login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(login);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

Write a custom drawable using File--> New--> VectorAsset https://developer.android.com/studio/write/vector-asset-studio.html Choose the arrow accordingly and Enable the field -->(Enable the auto mirroring for RTL Layout)

Auto-Mirror Property checked

I have changed the color of Arrow to be White, You can choose your own color

FileName: ic_arrow_back_black_24dp.xml in drawable folder

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:autoMirrored="true"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#00000000"
        android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
</vector>

After changing the Locale to Arabic - which changes the entire Layout from RTL to LTR

like image 184
KiranSrinivasRao Avatar answered Nov 15 '22 00:11

KiranSrinivasRao


I just added a drawable icon that rotated 180 degree and set the icon in onCreate() but the problem is we should do this in every activity that It's layout is RTL. So if our application supports both directions we should check if layout direction is RTL then change the up indicator:

if(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_up_rtl);
}
like image 37
Hamidreza Salehi Avatar answered Nov 14 '22 22:11

Hamidreza Salehi