Showing posts with label language-code. Show all posts
Showing posts with label language-code. Show all posts

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.

Language Code In Every Country In The World

Language ISO Code
Abkhazian ab
Afar aa
Afrikaans af
Albanian sq
Amharic am
Arabic ar
Aragonese an
Armenian hy
Assamese as
Aymara ay
Azerbaijani az
Bashkir ba
Basque eu
Bengali (Bangla) bn
Bhutani dz
Bihari bh
Bislama bi
Breton br
Bulgarian bg
Burmese my
Byelorussian (Belarusian) be
Cambodian km
Catalan ca
Cherokee  
Chewa  
Chinese zh
Chinese (Simplified) zh-Hans
Chinese (Traditional) zh-Hant
Corsican co
Croatian hr
Czech cs
Danish da
Divehi  
Dutch nl
Edo  
English en
Esperanto eo
Estonian et
Faeroese fo
Farsi fa
Fiji fj
Finnish fi
Flemish  
French fr
Frisian fy
Fulfulde  
Galician gl
Gaelic (Scottish) gd
Gaelic (Manx) gv
Georgian ka
German de
Greek el
Greenlandic kl
Guarani gn
Gujarati gu
Haitian Creole ht
Hausa ha
Hawaiian  
Hebrew he, iw
Hindi hi
Hungarian hu
Ibibio  
Icelandic is
Ido io
Igbo  
Indonesian id, in
Interlingua ia
Interlingue ie
Inuktitut iu
Inupiak ik
Irish ga
Italian it
Japanese ja
Javanese jv
Kannada kn
Kanuri  
Kashmiri ks
Kazakh kk
Kinyarwanda (Ruanda) rw
Kirghiz ky
Kirundi (Rundi) rn
Konkani  
Korean ko
Kurdish ku
Laothian lo
Latin la
Latvian (Lettish) lv
Limburgish ( Limburger) li
Lingala ln
Lithuanian lt
Macedonian mk
Malagasy mg
Malay ms
Malayalam ml
Maltese mt
Maori mi
Marathi mr
Moldavian mo
Mongolian mn
Nauru na
Nepali ne
Norwegian no
Occitan oc
Oriya or
Oromo (Afaan Oromo) om
Papiamentu  
Pashto (Pushto) ps
Polish pl
Portuguese pt
Punjabi pa
Quechua qu
Rhaeto-Romance rm
Romanian ro
Russian ru
Sami (Lappish)  
Samoan sm
Sangro sg
Sanskrit sa
Serbian sr
Serbo-Croatian sh
Sesotho st
Setswana tn
Shona sn
Sichuan Yi ii
Sindhi sd
Sinhalese si
Siswati ss
Slovak sk
Slovenian sl
Somali so
Spanish es
Sundanese su
Swahili (Kiswahili) sw
Swedish sv
Syriac  
Tagalog tl
Tajik tg
Tamazight  
Tamil ta
Tatar tt
Telugu te
Thai th
Tibetan bo
Tigrinya ti
Tonga to
Tsonga ts
Turkish tr
Turkmen tk
Twi tw
Uighur ug
Ukrainian uk
Urdu ur
Uzbek uz
Venda  
Vietnamese vi
Volapük vo
Wallon wa
Welsh cy
Wolof wo
Xhosa xh
Yi  
Yiddish yi, ji
Yoruba yo
Zulu zu