In your app, you can add as numerous languages as you want. By languages, I mean the language of the textbook that we see in any mobile operation. For Example, if you have English, French, and Hindi druggies, also you can have language support for all these languages. Like” Hello” in English will come ” Bonjour” in French and ” नमस्ते” in Hindi.
But how can we change the language? That is the content for this blog. In this blog, we will learn how to change the App locale in Android with ease.
By dereliction, you have English as your dereliction language for the whole app and the textbook corresponding to the English language will be there in our strings.xml train. So, also, for other languages, you need to add their strings.xml train.
So, principally Android loads the coffers related to language grounded on the system locale setting. For illustration, if the language of your Android phone is English, also strings.xml of English will be loaded by Android. We don’t have to do anything redundant then.
But, numerous times, we want our druggies to change the language of our App only by opting the favored language and not by changing the language of our Android phone. So, let’s see how to add this.
First of all, produce a design in Android Studio. Then, we will be having 2 conditioning
- MainActivity : This will contain one welcome TextView and two Buttons, one for opening the coming exertion in the English language and the other for opening the coming exertion in the Hindi language.
- LanguageActivity : This will simply contain a TextView displaying some communication. The communication will be shown in English if the language of the app is English and the communication will be shown in Hindi if the language of the app is Hindi.
So, let’s start with the MainActivity. The following is the law for the activity_main.xml train
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/tvWelcome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="Welcome Bro.." android:textAlignment="center" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnEnglish" android:layout_width="match_parent" android:layout_height="56dp" android:layout_marginTop="40dp" android:text="English" android:textAllCaps="false" app:layout_constraintEnd_toEndOf="@id/tvWelcome" app:layout_constraintStart_toStartOf="@id/tvWelcome" app:layout_constraintTop_toBottomOf="@id/tvWelcome" /> <Button android:id="@+id/btnHindi" android:layout_width="match_parent" android:layout_height="56dp" android:layout_marginTop="16dp" android:text="Hindi" android:textAllCaps="false" app:layout_constraintEnd_toEndOf="@id/tvWelcome" app:layout_constraintStart_toStartOf="@id/tvWelcome" app:layout_constraintTop_toBottomOf="@id/btnEnglish" /> </androidx.constraintlayout.widget.ConstraintLayout>
Then, we’ve 1 TextView and 2 Buttons.
also, you can add one TextView in your LanguageActivity. The following is the XML law for the same.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".LanguageActivity"> <TextView android:id="@+id/tvLearning" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Message Learning" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
And finally, here is the strings.xml file:
<resources> <string name="app_name">ChangeAppLocale</string> </resources>
So, we’re done with our prerequisite part. Now, we’re ready to add a new language to our app. Follow the below way to do so
- Right Click on res> New> Android Resource train
- Put the train name as strings.xml
- Select the option of Locale
- Select the locale for Hindi with code “hi”
- Select the locale for Hindi with law” hi” Click on Ok and now, you’ll be having two values directory i.e. the dereliction one with the name values and the other with name values hi. The dereliction one will contain the strings.xml train for the dereliction language( in our case, English) and the values- hi will contain the strings.xml train for the Hindi language.
Now, you need to put all the values of all the strings that we defined in our dereliction strings.xml train for our new Hindi strings.xml train.
But there are certain cases where we want to keep the textbook of the dereliction language. For illustration, the name of the App should be the same for all languages. In our case, we just need to restate the communication that’s shown on the LanguageActivity and not all textbooks like AppName, WelcomeMessage,etc.
So, for that, we need to set translatable to false in our dereliction strings.xml train( the English bone ).
For Example :
<string name="app_name" translatable="false">ChangeLocaleApp</string>
You can set translatable = ” false” for other string values as well. Now, for the other strings that you want to restate, you can simply add their values in the strings.xml train of the Hindi language. For illustration, the law of strings.xml(hi) will be:
<resources> <string name="msg_learning">सभी को नमस्कार! मैं ऐप लोकेल को बदलना सीख रहा हूं। यह सीखने में मज़ा रहा है।</string> </resources>
In this way, you’re telling the Android app to use the string value according to the locale of the App.
Now, our final task is to change the locale or simply the language of the app when we click on the button. For this, add the below function in your MainActivity.kt
fun setAppLocale(context: Context, language: String) { val locale = Locale(language) Locale.setDefault(locale) val config = context.resources.configuration config.setLocale(locale) context.createConfigurationContext(config) context.resources.updateConfiguration(config, context.resources.displayMetrics) }
In the below law, pass the environment and the language law and also start the LanguageActivity.
For Example :
engBtn.setOnClickListener { setAppLocale(this, "en") val intent = Intent(this, LanguageActivity::class.java) startActivity(intent) } hiBtn.setOnClickListener { setAppLocale(this, "hi") val intent = Intent(this, LanguageActivity::class.java) startActivity(intent) }
Now, when you open the LangaugeActivity, also you can see the changes in the language.
Points to keep in mind :
- Whenever an exertion is loaded also the corresponding string.xml train is loaded at that time only. For Example, if the current locale of your app is English and on button click, you changed it to Hindi. also no change on that particular exertion will be observed because the string train of that exertion is formerly loaded. But if you move to some other exertion and also come back to the first exertion, also you’ll see the language changes. However, also you need to refresh the exertion after the change of the locale, If you want to see instant changes.
- In some Android performances, you might not see the locale change if you’re opening some WebView in your app. Always take care of the case when you load the WebView for the veritably first time. One possible option is to set again the locale after loading the WebView.
- You need to handle the case of exposure change of your exertion. The locale will be reset in this case as well, so handle this as well.
- It’s recommended by Android to not change the locale at runtime. So, you can avoid this if the use- case of language isn’t that important important in your app.