How to Change Language Programmatically in Android App – Building Multi-Language Supported App

Please follow and like us:

How to Change Language Programmatically in Android App – Building Multi-Language Supported App

Hello guys, looking for the feature that allows to change language programmatically in your android app and make your app support multiple languages? then you are the right place. In this tutorial, we will able to change language within the app. Let’s see how to build multi-language supported app step by step.

Step by Step: Build Multiple Language Supported Android App

Note: Generally in google, I found the solution of multiple language support demo which is working only if we change language from android device’s settings. but for changing language programmatically is working for only few default languages. not for all languages.

But here I used some trick to support any language in android app.

In my example, I did multi support for languages like English, Hindi, and Gujarati. where English is available for localization but for Hindi and Gujarati I used different language’s name attributes

What trick I used for support any language in Android App.

Due to very few Default Locale available in Android so we have to use it like bellow
res –> values–>strings.XML (For the English Language)

res –> values-de –> strings.XML (I used For the Gujarati Language). Here you can use any your language.
values-de (normally used for german language). Here you can use any your language.

res –> values-fr –> strings.xml (I used For the Hindi Language)
values-fr (normally used for french language)

Let’s understand the logic of setup multiple language support in Android App.

Step 1: Create Layout files and declare string resources in strings.xml file

</pre>
<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_change_language"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
    android:background="@color/bg_gradient_end"
    tools:context="com.nkdroid.multi.languages.demoapp.ChangeLanguageActivity">

    <TextView
        android:id="@+id/txtChooseLanguage"
        android:padding="16dp"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="#fff"
        android:gravity="center"
    android:text="Choose your language"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <LinearLayout
        android:id="@+id/llEnglish"
        android:clickable="true"
        android:background="@drawable/rectangle_shape"
        android:padding="10dp"
        android:gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView

            android:paddingLeft="16dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"


            android:text="ENGLISH"

            android:textColor="@color/colorAccent"
            android:textSize="16sp" />
        <ImageView
            android:id="@+id/imgSelect1"
            android:visibility="gone"
            android:src="@drawable/ic_selected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/llHindi"
        android:clickable="true"
        android:layout_marginTop="16dp"
        android:background="@drawable/rectangle_shape"
        android:padding="10dp"
        android:gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView

            android:paddingLeft="16dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="हिंदी"

            android:textColor="@color/colorAccent"
            android:textSize="16sp" />
        <ImageView
            android:visibility="gone"
            android:id="@+id/imgSelect2"
            android:src="@drawable/ic_selected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>



    <LinearLayout
        android:id="@+id/llGujarati"
        android:clickable="true"
        android:layout_marginTop="16dp"
        android:background="@drawable/rectangle_shape"
        android:padding="10dp"
        android:gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView

            android:paddingLeft="16dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"


            android:text="ગુજરાતી"

            android:textColor="@color/colorAccent"
            android:textSize="16sp" />
        <ImageView
            android:visibility="gone"
            android:id="@+id/imgSelect3"
            android:src="@drawable/ic_selected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:layout_margin="32dp"

        android:id="@+id/btnUpdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:background="@drawable/rectangle_shape"
        android:text="UPDATE"
        android:textAllCaps="false"
        android:textColor="@color/colorAccent"
        android:textSize="15dp" />

</LinearLayout></pre>
<pre>

Step 2: Create separate values folders for multiple language support

res –> values–>strings.XML (For the English Language)

<resources>
  <string name="app_name">MultiLanguageDemo</string>

  <string name="welcome">Welcome</string>
</resources>

res –> values-de –> strings.XML (I used For the Gujarati Language).

<resources>
  <string name="app_name">MultiLanguageDemo</string>
  <string name="welcome">આપનું સ્વાગત છે</string>
</resources>

res –> values-fr –> strings.XML (I used For the Hindi Language)

<resources>
  <string name="app_name">MultiLanguageDemo</string>
  <string name="welcome">स्वागत हे</string>
</resources>

Maybe you have doubt why I haven’t put change_language_activity.xml layout’s resources in strings.xml

Because we can’t convert in multiple languages of current activity at the time of allying logic of muli-language support. so for that activity, we have done language support programmatically.

Step 3: Main logic for supporting multiple languages in Android App Programmatically.

package com.nkdroid.multi.languages.demoapp;

import android.content.Intent;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nkdroid.multi.languages.demoapp.custom.PrefUtils;
import com.nkdroid.multi.languages.demoapp.model.AppConstants;
import com.nkdroid.multi.languages.demoapp.model.LanguageType;

import java.util.Locale;

public class ChangeLanguageActivity extends AppCompatActivity {

    private LinearLayout llGujarati, llHindi, llEnglish;
    private ImageView imgSelect2, imgSelect3, imgSelect1;

    private TextView txtChooseLanguage;

    private Button btnUpdate;

    private String selectedLanguage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_language);

        btnUpdate = (Button) findViewById(R.id.btnUpdate);

        txtChooseLanguage = (TextView) findViewById(R.id.txtChooseLanguage);

        try {
            selectedLanguage = PrefUtils.getLanguage(ChangeLanguageActivity.this).languageType;
            Log.e("Selected language", selectedLanguage);
        } catch (Exception e) {
            Log.e("eeror", e + "");
            selectedLanguage = AppConstants.English;
            e.printStackTrace();
        }


        llGujarati = (LinearLayout) findViewById(R.id.llGujarati);
        llHindi = (LinearLayout) findViewById(R.id.llHindi);
        llEnglish = (LinearLayout) findViewById(R.id.llEnglish);

        imgSelect1 = (ImageView) findViewById(R.id.imgSelect1);
        imgSelect2 = (ImageView) findViewById(R.id.imgSelect2);
        imgSelect3 = (ImageView) findViewById(R.id.imgSelect3);

        updateData();

        llGujarati.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgSelect1.setVisibility(View.GONE);
                imgSelect2.setVisibility(View.GONE);
                imgSelect3.setVisibility(View.VISIBLE);
                txtChooseLanguage.setText("તમારી ભાસા પસંદ કરો");
                btnUpdate.setText("અપડેટ");
                selectedLanguage = AppConstants.GUJARATI;
            }
        });

        llHindi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                imgSelect1.setVisibility(View.GONE);
                imgSelect2.setVisibility(View.VISIBLE);
                imgSelect3.setVisibility(View.GONE);
                txtChooseLanguage.setText("अपनी भाषा चुनें");
                btnUpdate.setText("अपडेट");
                selectedLanguage = AppConstants.HINDI;
            }
        });

        llEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgSelect1.setVisibility(View.VISIBLE);
                imgSelect2.setVisibility(View.GONE);
                imgSelect3.setVisibility(View.GONE);
                txtChooseLanguage.setText("Choose your language");
                btnUpdate.setText("UPDATE");
                selectedLanguage = AppConstants.English;
            }
        });

        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LanguageType languageType = new LanguageType();
                if (selectedLanguage.equalsIgnoreCase(AppConstants.English)) {
                    languageType.languageType = AppConstants.English;
                    PrefUtils.setLanguage(languageType, ChangeLanguageActivity.this);
                    Log.e("Selected language", PrefUtils.getLanguage(ChangeLanguageActivity.this).languageType);
                    Configuration config = new Configuration();
                    config.locale = Locale.ENGLISH;
                    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
                } else if (selectedLanguage.equalsIgnoreCase(AppConstants.HINDI)) {
                    languageType.languageType = AppConstants.HINDI;
                    PrefUtils.setLanguage(languageType, ChangeLanguageActivity.this);
                    Log.e("Selected language", PrefUtils.getLanguage(ChangeLanguageActivity.this).languageType);
                    Configuration config = new Configuration();
                    config.locale = Locale.FRENCH;
                    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
                } else if (selectedLanguage.equalsIgnoreCase(AppConstants.GUJARATI)) {
                    languageType.languageType = AppConstants.GUJARATI;
                    PrefUtils.setLanguage(languageType, ChangeLanguageActivity.this);

                    Log.e("Selected language", PrefUtils.getLanguage(ChangeLanguageActivity.this).languageType);
                    Configuration config = new Configuration();
                    config.locale = Locale.GERMANY;
                    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
                }


                Intent i = new Intent(ChangeLanguageActivity.this, HomeActivity.class);
                startActivity(i);
                finish();
            }
        });

    }

    private void updateData() {
        if (selectedLanguage.equalsIgnoreCase(AppConstants.English)) {
            imgSelect1.setVisibility(View.VISIBLE);
            imgSelect2.setVisibility(View.GONE);
            imgSelect3.setVisibility(View.GONE);
            txtChooseLanguage.setText("Choose your language");
            btnUpdate.setText("UPDATE");

        } else if (selectedLanguage.equalsIgnoreCase(AppConstants.HINDI)) {
            imgSelect1.setVisibility(View.GONE);
            imgSelect2.setVisibility(View.VISIBLE);
            imgSelect3.setVisibility(View.GONE);
            txtChooseLanguage.setText("अपनी भाषा चुनें");
            btnUpdate.setText("अपडेट");
        } else if (selectedLanguage.equalsIgnoreCase(AppConstants.GUJARATI)) {
            imgSelect1.setVisibility(View.GONE);
            imgSelect2.setVisibility(View.GONE);
            imgSelect3.setVisibility(View.VISIBLE);
            txtChooseLanguage.setText("તમારી ભાસા પસંદ કરો");
            btnUpdate.setText("અપડેટ");
        }
    }
}

Hope you liked our simple tutorial regarding support multiple languages in Android App programmatically. stay connected for an upcoming tutorial in the Android world.

nirav kalola