Step 1: Create A New Project and Go to app > res > values > right-click > New > Value Resource File and name it. Select qualifiers as Locale from the available li
st. Follow steps
Step 2: Now in the string_bn
<resources>
<string name="app_name">Language Change</string></resources><string name="header_text" >"Language Bangla"</string>
Step 3: in the string.xml
<resources>
<string name="app_name">Language Change</string></resources><string name="header_text" >"Language English"</string>
Step 4: XML
<LinearLayout
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"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="48dp"
android:text="@string/header_text"
android:textAlignment="center" />
<RadioGroup
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/btnBangla"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bangla"/>
<RadioButton
android:checked="true"
android:id="@+id/btnEnglish"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"/>
</RadioGroup>
</LinearLayout>
Step 4: Create a Java class and name It as LocaleHelper
package com.tutorialb.languagechange;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context setLocale(Context context, String language) {
persist(context, language);
return updateResources(context, language);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
Step 4: MainActivity.java
package com.tutorialb.languagechange;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
TextView messageView;
RadioButton btnBangla, btnEnglish;
Context context;
Resources resources;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageView = findViewById(R.id.textView);
btnBangla = findViewById(R.id.btnBangla);
btnEnglish = findViewById(R.id.btnEnglish);
btnEnglish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context = LocaleHelper.setLocale(MainActivity.this, "en");
resources = context.getResources();
messageView.setText(resources.getString(R.string.header_text));
}
});
btnBangla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context = LocaleHelper.setLocale(MainActivity.this, "bn");
resources = context.getResources();
messageView.setText(resources.getString(R.string.header_text));
}
});
}
}
Step 5: Output