Saturday, December 24, 2016

Android Studio - Change Language (Locale) On Application Without Open Menu Setting



Today i going to show you how to change application's language without open menu setting on your device.

In this tutorial, even you force stop your application, the language won't change. For example if you choose Indonesian, after that do force stop, the language will still Indonesian on your Apps.

VIDEO DEMO



In this case i use to tow languages, but you can provide with much languages in your application.


PREPARATION

After you created the project, the next thing that you do is create the folder values for your language. For example :

In this case (this tutorial) :

There is a default folder values (for English), i need to translate this apps to Bahasa Indonesia. So i create new folder values, for Indonesia i have to create values-in. -in is Indonesian language's code. You can check your language code HERE

After your create the folder values-yourCountryCode, inside this folder create new strings.xml. The output look like the following Figure 1:

Figure 1
Explanation refer on the number of Figure 1 :

  1. In order to easy you create the new values folder, just change from Android to  project 
  2. Right  click on res folder, create new directory, and give it name values-yourLanguageCode (in my case : values-in)
  3. The default values folder
  4. The values folder that you created will display here.

IMPLEMENTATION

Now create your apps as usual, follow the steps below :

Create some layouts :
  1. activity_splash_screen.xml
  2. activity_before_main.xml
  3. activity_main.xml
  4. activity_second.xml
Create some java classes :
  1. SplashScreenActivity.java
  2. BeforeMainActivity.java
  3. MainActivity.java
  4. SecondActivity.java
Note :
In every give text/label in Button, TextView, EditText, etc, please save the string inside strings.xml

Now modify every layouts :

Open activity_splash_screen.xml, paste the following code :

Open activity_before_main.xml, paste the following code :

Open activity_main.xml, paste the following code :

Open activity_second.xml, paste the following code :

You have finished with the layouts, Remember you have to store the string inside strings.xml (@string/your_label), for example please take a look at the following Figure 2 :

Figure 2
Next modify all of java classes.

Open SplashScreenActivity.java, paste the following code :

The splash screen created by Thread, it will appears for 2 seconds (you can change the timer). After that please take a look of this method  getCurrentLocale() . That method used to detect the change of the locale.

If there is any change, the configuration will change the language of the apps. Please take a look of the method : onConfigurationChanged(Configuration newConfig) . That method will change the language if there is any change detected by method getCurrentLocale()

Open BeforeMainActivity.java, paste the following code :

Open MainActivity.java, paste the following code :

In MainActivity.java, there is a spinner that contains the name of any languages. In every click on spinner's item, get the configuration and change the locale (Method changeLanguage(String lang)).

Inside the method changeLanguage(String lang)  beside we'll change the locale, also will save the locale on sharedPreference and then refresh the activity.

onConfigurationChanged(Configuration newConfig) will change the configuration so the language will translated.

onKeyDown(int keyCode, KeyEvent event), this method used to refresh the previous activity when user tap the back button.

Why use onKeyDown(int keyCode, KeyEvent event)?

Because if use onBackPressed() won't refresh the previous activity, so we use intent inside onKeyDown(int keyCode, KeyEvent event).

Open SecondActivity.java, paste the following code :


You have finished with your java classes, now please take a look in last several steps below :

Give this android:configChanges="locale" in every activity in your AndroidManifest.xml

Copy the strings.xml's value from strings.xml inside folder values to strings.xml inside folder values-yourLanguageCode (in my case values-in).

Done!! even you force stop your application, it won't change the language to the default. Hope it helps. Thank you.

Related Posts


EmoticonEmoticon