Mixalis Mixalis - 5 months ago 12
Java Question

Change languange in android studio

I try to learn how to program in android studio and i am currently in the second tutorial about how to change languange by localizing.
When i try to create a second strings.xml file inside the folder values_el it say me i cant cause names should be unique.
I try to copy the original strings.xml file from values folder to the new values_el folder, i translate the messages but nothing happens.
Also i try to right click the original strings.xml file and i press the translate option and i translate them from there but nothing happens again.
When i run the app in my phone the languange is english in both of the ways i try above.
My phone languange is Greek but the letters of my program is english.

So here is 2 questions.
First why the languange do not change in my phone?
Second is there a way to change the languange of my app by pressing a button while i open it? Some games i play from google play have the option to choise your languange while before you start the game.
Sorry about my english if you dont understant something i say please let me know so i try to explain it better with google translate help.
Thank for your time anyway.

Thats the code i run

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my"
android:orientation="horizontal">

<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/edit_message" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test"
android:onClick="testActivity" />

</LinearLayout>

Answer

Why dont you use the easy way through the IDE?

  1. Open your strings.xml file
  2. Click the Open Editor link as seen below

enter image description here

  1. Add the locales you want to add translations for by selecting like this

enter image description here

  1. This will create the right file for you without you needing to worry about the file name

  2. You dont need to open each strings.xml to put the localized strings. Do it right from this strings.xml editor.

Coming to your second question: The local on app will be the locale selected on device in the device settings. If you have not localized for the device locale it will fall back to the main strings.xml under res/values.

To force a locale in your app irrespective of the device's locale:

Add the following method in your activity class and call it in onCreate

private void setLanguageForApp(String language){

    String languageToLoad  = language; //pass the language code as param
    Locale locale;
    if(languageToLoad.equals("not-set")){
        locale = Locale.getDefault();
    }
    else {
        locale = new Locale(languageToLoad);
    }
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}