Android Building Multi-Language Supported App - Androhub

Multilingual Banner

Android Building Multi-Language Supported App

Android is one of the few popular mobile operating systems having millions of users over 190 countries and growing day by day. So when you are aiming your app to be globally successful, it is always a good idea to make the app localised by supporting different languages.

While localising, you should consider using appropriate text, audio, currency, numbers and graphics depending upon the region or country. But this tutorial only covers localising strings i.e supporting multiple languagesLocalizing with Resources explains about other things should be considered when localizing your app.

How String Localization Works

By default android considers English as primary language and loads the string resources from res ⇒ values ⇒ strings.xml. When you want to add support for another language, you need to create a values folder by appending an Hyphen and the ISO language code. For example if you want to add support for Russian, you should create a values folder named values-ru under res directory and keep a strings.xml file in it with all the strings translated into Russian language.

In brief the localization works as follows :

1. When user changes the device language through Settings ⇒ Language & Input, android OS itself checks for appropriate language resources in the app. (Let’s say user is selecting Russian)

or

If you don’t want to change language from settings then you have to create language options in your app and save that locale in Shared Preferences for future use. (This method will change language of your app only not whole device like above one.)

2. If the app supports selected language, android looks for it’s string resources in values-(ISO language Code) folder in the project. (For russian it loads the string values from values-ru/string.xml)

3. If the supported language strings.xml misses any string value, android always loads the missing strings from default strings.xml file i.e values/strings.xml

So it is mandatory that the default stings.xml file should contains all the string values that app uses. Other wise the app will crash with Force Close error.

Below are two things that you have to follow:

1. Never hard code the string in xml or in java code which make the translation difficult.

2. While you are supporting multiple languages, you should consider below as a best practice while defining the strings. Always declare the string in strings.xml only.

When referring declared strings in xml, use @string notation.

When defining the string through java code, use R.string

Example

In this tutorial, we are going to build a Multi-Language (Multilingual) supported app that supports EnglishFrench, Deutsch (German), Hindi and Russian.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open strings.xml located under res=>values folder and add following strings. These are default English language strings.

3. Now under res folder create three folders named values-de, values-fr, values-hi, values-ru and a strings.xml file in each of the folders.

Your project should look like this once you created the required files/folders.

Multilingual App String Values
Multilingual App String Values

Now translate the strings into their respected languages and place them in appropriate strings.xml files You can use Google Translator for strings conversion.

  • Hindivalues-hi/strings.xml

  • Russian – values-ru/strings.xml

  • Deutsch(German) – values-de/strings.xml

  • French – values-fr/strings.xml

4. Now create a layout naming activity_main.xml and add the following code. In this there are several buttons for different language selection.

5. Finally come to your MainActivity.java and add the following code. In this we are saving the selected language in SharedPreferences and loading saved language by refreshing texts.

The below method is important for changing selected language.

Full MainActivtiy.java Source Code

6. Finally, run the application and you will get the output as shown in video.

Thanks. :)

 

26 Comments

byron
Tuesday, March 14th, 2017

Dear Dr. Droid,
Thank you, thank you, THANK YOU for your amazing work! You just helped me a great deal. Pls kindly give directions on how to implement this in a project with more than one activity? Your help is much appreciated. Thanks again.

Dr. Droid
Tuesday, March 14th, 2017

Hi Byron,

To implement the string localisation in whole app/project you need to re-start the app again. Like a button in your app is changing language then on your button click you need to call below method:

Intent i = new Intent(Your_Activity.this, Splash.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();

Thanks

byron
Wednesday, March 15th, 2017

Dear Dr. Droid,
Thanks for your quick reply. I am assuming that “splash.class” is just an example? Also, can i put your new code inside your “changeLocale” method? it will look something like this;

public void changeLocale(String lang) {
if (lang.equalsIgnoreCase(“”))
return;
myLocale = new Locale(lang);//Set Selected Locale
saveLocale(lang);//Save the selected locale
Locale.setDefault(myLocale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = myLocale;//set config locale as selected locale
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();//Update texts according to locale
Intent i = new Intent(Your_Activity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}

Also, what about extending your mainactivity from other activities?
i tried it when i added a second activity to your project, and it seemed to work well. is it safe to do this?
I ask because in my project, most of the activities are using different color themes. if it will work, perhaps i can make your project a “settings” page and extend it from other activities? Thanks again, Dr. Droid, for your valued help.

Dr. Droid
Wednesday, March 15th, 2017

Hi Byron,

Below are the answers of your queries:

1. I am assuming that “splash.class” is just an example? – YES
2. Do not call this updateTexts(); method because as you are restarting app so this method no more required to call.
3. i added a second activity to your project, and it seemed to work well. is it safe to do this? – YES
4. In manifest you can change the theme of my project activity if it is not matching with yours.

Thanks

Rupin Sahu
Friday, July 28th, 2017

Hi Dr. Droid, it was great to see your knowledge on Android. I need your help as I am preparing a project and I am also a freelancer and very keen to learn new things. In my project I want multilingual feature but as you said that there is another method without android localisation means I do not want to change the language from device’s settings so I want to try your alternate method as you defined in your point no. 1. So how could it be done and what will be the code? Please help me.

Samir
Sunday, August 27th, 2017

hello
how are ?
how to implement this in a project with more than one activity?
if we use 3 activity

Samir
Sunday, August 27th, 2017

and what we need to work for all activity this change

Dr. Droid
Sunday, August 27th, 2017

Hi Samir,

You have to use this changeLocale(String lang) method to update the language of your app. This method you need to put into your first activity like your SplashActivity or your MainActivity rest android will handle itself to show particular changed language.

Thanks

Samir
Sunday, August 27th, 2017

very thanks my bro to answer me
if send me to i put with first activity

thanks

Samir
Sunday, August 27th, 2017

if upload code for me my bro is very good
to all activity i want use for some activity

thanks

Dr. Droid
Sunday, August 27th, 2017

Hi Samir,

Put the below code to your starting activity and call it onCreate method. You need to pass a locale(language) that you want to change to this method and also create a shared preference so that whenever you start your app again it will pick the saved locale.

//Change Locale
public void changeLocale(String lang) {
if (lang.equalsIgnoreCase(“”))
return;
myLocale = new Locale(lang);//Set Selected Locale
saveLocale(lang);//Save the selected locale
Locale.setDefault(myLocale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = myLocale;//set config locale as selected locale
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());//Update the config
updateTexts();//Update texts according to locale
}

Try this, if you face any issue let me know.

Thanks

Samir
Sunday, August 27th, 2017

my bro i’m very try not succeeded
if make for me and send very thanks

Dr. Droid
Sunday, August 27th, 2017

Hi Samir,

Can you share your code with me or can you connect with me via TeamViewer so that i can help you properly.
My email id is at my blog.

Thanks

Samir
Sunday, August 27th, 2017

Hi
my bro tomorrow i send my code for you

paresh
Wednesday, November 8th, 2017

brother, i got your locale, and know the concept of Typeface, but i am not suceeding with Marathi language or Hindi language printing through android bluetooth printing, can you help me please

Dr. Droid
Wednesday, November 8th, 2017

Hi Paresh,

The problem of not printing Hindi or Marathi language can be that the printing device is not supporting those languages.

If that is not the case then let me know how i can help you.

Thanks

Abhijit Lenka
Tuesday, July 31st, 2018

Hi Sonu,

I need your help urgently. I have the issue with language again set back to default upon exiting the app.

Thanks
Abhijit Lenka
mail_to:[email protected]

Dr. Droid
Tuesday, July 31st, 2018

Hi Abhijit,

Can you tell me what exactly you are doing and if possible share your code where you are facing issue.

Thanks

Omkar Kamate
Wednesday, August 8th, 2018

I am not getting how can I use this for multiple activities. Can u modify my project and show me

Dr. Droid
Wednesday, August 8th, 2018

Hi Omkar,

You have to use changeLocale and loadLocale method in your splash or launching activity and changeLocale and save locale inside your changing locale activity. Once you change the locale restart your app to see the effects.

Thanks

Ansa
Monday, December 24th, 2018

english.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Main2Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
}); set the second activity on onclicklistner of english button but language of second activity doesnt change.Please help

Farahnaz Reza
Monday, January 21st, 2019

Whenever i insert this code in my project i get the following error Binary XML file line #130: Binary XML file line #130: You must supply a layout_width attribute. and the app crashes. Can you please help me debug.?

mispruebas
Saturday, April 27th, 2019

Hello friends, thanks for the contribution, very good tutorial, I have a problem, when I select the button for the change of language, the application changes the language without problems, but the action bar title of the open activity did not change. Can you help me with that?
for the moment I used a restartActiviti ();
thanks greetings!!

Dr. Droid
Sunday, April 28th, 2019

Hi Mispruebas,

You have to restart your application to see the changes in whole app.

Thanks

Dheenadhayalan
Thursday, February 13th, 2020

Its working on one app . i used same code but that app is not working

Dr. Droid
Friday, February 14th, 2020

Hi Dheenadhayalan,

Can you check your code once again and check if anything you missed?
And also can you check if you are getting any error logs?

Thanks

Post comment

Your email address will not be published. Required fields are marked *