• Home
  • Android
  • Programming
    • Object Oriented Programming
  • PHP
  • Contact

bitcaal.com

  • Home
  • Android
  • Programming
    • Object Oriented Programming
  • PHP
  • Contact

How to Change The App Language Programmatically in Android

bymehadi February 26, 2020 Android Basic

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.

Table Of Contents

  1. Creating New Project in Android Studio
  2. Add new resource file strings.xml
  3. Code for main_activity.xml
  4. Code for MainActivity.java

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

Object-Oriented Programming Concepts: Properties and Methods

5 Best New Latest Technology

1 thought on “How to Change The App Language Programmatically in Android”

  1. JohnDoes
    April 29, 2020 at 5:37 am

    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.

    Reply
Leave a Reply Cancel reply

19 − 10 =

Recent Posts

  • Remove null values from an Array in PHP
  • Rubik’s Cube, 3D Combination Puzzle
  • Object-Oriented Programming Concepts: Constructor & Destructor
  • 5 Best New Latest Technology
  • How to Change The App Language Programmatically in Android

Categories

  • Android Basic
  • Object Oriented Programming
  • PHP
  • Programming
  • Technology

Recent Comments

  • JohnDoes on How to Change The App Language Programmatically in Android
© bitcaal 2020