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.
In this case i use to tow languages, but you can provide with much languages in your application.
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:
- In order to easy you create the new values folder, just change from Android to project
- Right click on res folder, create new directory, and give it name values-yourLanguageCode (in my case : values-in)
- The default values folder
- The values folder that you created will display here.
Now create your apps as usual, follow the steps below :
Create some layouts :
Create some java classes :
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 :
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.