How to Change The App Language Programmatically in Android
In this tutorial i will show you how you can change your app language programmatically in android. It’s very important to make any application to support multi language. Multiple language support will help you to get more users to your app. Let’s jump into the main content.
Creating New Project in Android Studio
In Android Studio, Go to File Menu -> New Project -> and fill up the required information then click Finish.
Add new resource file strings.xml
Goto res folder and right click on it. Select New -> Android Resource File and follow the below image.
You can create many language as per your need.
Create a string entry for English locale
Create a string entry for Bangla locale
Code for main_activity.xml
Add this code to activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/love_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code for MainActivity.java
Add this code to MainActivity.java
package me.mehadih.multiplelanguage;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setApplicationLocale("bn");
setContentView(R.layout.activity_main);
}
private void setApplicationLocale(String locale) {
Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(new Locale(locale.toLowerCase()));
} else {
config.locale = new Locale(locale.toLowerCase());
}
resources.updateConfiguration(config, dm);
}
}
Now run the app to view the changes.
Download Source Code From Here
1 thought on “How to Change The App Language Programmatically in Android”
This is a way to simplified solution for the problem and changes only the few things. The language output e.g. will still be the old locale.