Ofek Agmon
Ofek Agmon

Reputation: 5198

Changing Locale but keep left-to-right and other phone orientations

I have an app that is available in two languages - English and Hebrew.

I added Hebrew strings using the Translation Editor and I am changing the Locale according to the user selection.

When changing the Locale, it sets the strings to Hebrew like I wanted, but its also changes the toolbar orientation to right-to-left for Hebrew and brings the title and back-button to the right.

English Locale (Default):

enter image description here

Hebrew Locale: enter image description here

Is there a way to keep the toolbar orientation like the English one? I want to keep the back button and the title in the left of the toolbar.

Edit: after adding either android:layoutDirection="ltr" or android:supportsRtl="false" to the toolbar xml. arrow is backwards. how ti fix it?

enter image description here

Upvotes: 7

Views: 9986

Answers (4)

Hamza Rauf
Hamza Rauf

Reputation: 11

Use constraint Layout for handle this kind of issue, if you want to not change position of back button use app:layout_constraintLeft_toLeftOf="parent" OR app:layout_constraintRight_toRightOf="parent"

but if you want to change position of Button or any other element after changing Local you can use app:layout_constraintStart_toStartOf="parent" OR app:layout_constraintEnd_toEndOf="parent"

Upvotes: 0

Emre Aktürk
Emre Aktürk

Reputation: 3346

Change Locale but keep left-to-right and other phone orientations

Specifically, add android:supportsRtl="false" to the <application> element in your manifest file.

For more information Link

Upvotes: 12

Pegah Kiaei
Pegah Kiaei

Reputation: 43

I had this problem too. I had a method to change the locale of the language and the application configuration :

private String loadPreference() {
    SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    String language = shp.getString("Language","fa");
    Locale myLocale = new Locale(language);
    Locale.setDefault(myLocale);
    Configuration config = new Configuration();
    config.locale = myLocale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    String locale = getResources().getConfiguration().locale.getDisplayName();
    System.out.println(locale);
    return locale;
}

It is changing locale and simultaneously changing the layoutDirection, so you can solve this by setting the direction manually:

private String loadPreference() {
    SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    String language = shp.getString("Language","fa");
    Locale myLocale = new Locale(language);

    Configuration config = new Configuration();
    config.setLocale(myLocale);
    //manually set layout direction to a LTR location
    config.setLayoutDirection(new Locale("en"));
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    String locale = getResources().getConfiguration().locale.getDisplayName();

    return locale;
}

Upvotes: 3

daxgirl
daxgirl

Reputation: 772

Add android:layoutDirection="ltr" to your appbar layout. That will force ltr in any layout direction

Upvotes: 6

Related Questions