How to change the whole language in Android Programmatically

Android 7.0 provides support for multiple languages in our application. There are many types of language support in Android devices and the default language is English, and other many languages support.

We can change language programmatically on an Android device using the Locale object.

Android device applications use one or more countries so specific languages work in specific countries. 

Now create one example of two different languages in the application.         

Step 1: Create a new project, or if you have an existing project, then open it.

Step 2: We need to create a string resource file, so go to app > res > values > right-click> new value resource file and name. 


Step 3: Click on the>> button visible in the below dialog.

 

Step 4: Select the appropriate language, press an okay button, and create a different language file.

String(hi)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="selected_language">हिन्दी</string>
<string name="language">हैलो, क्या हाल हैं ?</string>
</resources>

String (En) This file is the default, we can not create it.

<resources>
<string name="selected_language">English</string>

<string name="language">Hi, How are you ?</string>
</resources>

Step 5: Create an activity_main.xml file for designing your screen.

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

// Textview used for label
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="@color/black"
android:textSize="20sp"
android:text="Change application language"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

//Textview used for lable
<TextView
android:id="@+id/txtmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:text="Welcome to Candidroot solutions"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

//Click on this button so change language of hindi
<Button
android:id="@+id/btnHindi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="Hindi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtmessage" />

//Click on this button so change language of English
<Button
android:id="@+id/btnEnglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="English"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtmessage" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 6: Create a MainActivity.java file for designing your screen.

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
Button btnHindi, btnEnglish;
TextView messageView;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

messageView = findViewById(R.id.txtmessage);
btnHindi = findViewById(R.id.btnHindi);
btnEnglish = findViewById(R.id.btnEnglish);


btnEnglish.setOnClickListener(view -> {
Context context = setLocale(MainActivity.this, "en");
Resources resources = context.getResources();
messageView.setText(resources.getString(R.string.language));
});

btnHindi.setOnClickListener(view -> {
Context context = setLocale(MainActivity.this, "hi");
Resources resources = context.getResources();
messageView.setText(resources.getString(R.string.language));
});
}

//Call this method set local
public static Context setLocale(Context context, String language) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}

return updateResourcesLegacy(context, language);
}

// This method is use for update resources
@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);
}

// This method is use for updateresourceslegacy
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;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}

resources.updateConfiguration(configuration, resources.getDisplayMetrics());

return context;
}

}

Happy coding!


365Bloggy May 9, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive