How to change whole app language in android programmatically
Hi Guys, Welcome to BD HELP ZONE , In this Android Tutorial we will create an app that can support multiple languages, In other words, the user can select his desired language to change the whole app language in android.
This is a Simple Example of how to make an android multi language app with an example – locale in android.
Video Tutorial on change whole app language android programmaticallyStep 1 − Create a new Android Project in Android Studio
Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. ( Name it as Change App language)
Step 2 – Create a new strings.xml with locale
In the values directory, you need to create a separate string.xml(bn) or any other language
Here is how to create a locale in android string with a language
right-click on values directory => New => Values Resource File
The below dialog will pop up in the android studio
Here you need to select your locale languages that your app will support.
After creating saperate strings.xml files for particular languages your string directory will look something like this.
Now in those strings.xml file add the translated strings
string.xml (hi) for hindi language in your android app.
string.xml (kn) for Kannada language in your app.
likewise you can add different language that you want to add into your android applicaton.
Step 3 – Create a LocaleHelper Class
Now create new java file and name it as Localehelper and add the below code.This class will help you in getting and storing language that a user has previously selected before he close the application, we will use SharedPreferences to store the locale selected by user in .
Step 4 – UI Design
activity_main.xml
Add the following code to res/layout/activity_main.xml.
@drawable/spinner_background.xml
create a new drawable resource file under drawable folder and add the below code.
This code is just to give background to above relativelayout
Create a vector drop image in drawable folder
Right Click(drawable) > New > Vector Image ( select a arrow down vector image )
Step 5 – Java Code to switch between string.xml to use
Main_Activity.java
Here in above code we have a RelativeLayout with OnClickEvent, Then user will click the RelativeLayout a AlertDialog will popup asking for language selection, by using which user can change language of his application.
For Example: When user select language as Hindi, The strings.xml (hi) will get loaded and all the text in the application will turn it language string that come from strings.xml (hi), likewise if user select kannada as his app language then strings gets loaded from strings.xml(kn).
and if he select language as English all language will come back to default string.xml
Conclusion
In this tutorial we learnt how to change whole app language in android programmatically using strings.xml locale.
Comments
Post a Comment