Android localization at runtime

In the previous post, the application we created identifies the locale before application is loaded. But in some situation where we need to change the language of the application at runtime or on the go according to the user’s need. You might have seen some application which has dialog or menu which list the languages supported by the application.

On selecting the language from the list, entire application will be applied with the locale/language selected which happens at runtime.

Follow the previous post example, where we are going to add spinner control to display the list of languages and add logic in activity class to handle the locale changes.
Either you can proceed with the below listings, or you can directly download code.


Quick Links

Code Listings

AndroidLocalize .java

  1. Declare spinner control, locale and button objects
  2. Find the spinner control from main.xml and add OnItemSelectedListener class to it
  3. Based on the list item selected, call setLocale method with the Locale value: English – en, Tamil – ta and Hindi – hi.
  4. Change the configuration of device (add locale) for your application
  5. Make sure that you refresh the current activity to reflect the changes made in configuration.

import java.util.Locale;
import android.os.Bundle;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AndroidLocalize extends Activity {
	Spinner spinnerctrl;
	Button btn;
	Locale myLocale;

	public void onCreate(Bundle savedInstanceState) {
		spinnerctrl = (Spinner) findViewById(;
		spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {

				if (pos == 1) {

							"You have selected Tamil", Toast.LENGTH_SHORT)
				} else if (pos == 2) {

							"You have selected Hindi", Toast.LENGTH_SHORT)
				} else if (pos == 3) {

							"You have selected English", Toast.LENGTH_SHORT)


			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub


	public void setLocale(String lang) {

		myLocale = new Locale(lang);
		Resources res = getResources();
		DisplayMetrics dm = res.getDisplayMetrics();
		Configuration conf = res.getConfiguration();
		conf.locale = myLocale;
		res.updateConfiguration(conf, dm);
		Intent refresh = new Intent(this, AndroidLocalize.class);


Congratulations, We are done.
Let us test the application:
Right click on the project >> Run as >> Android application >> Choose emulator or device
Tap on the button and see what happens.

Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.


I hope you enjoyed the post!! 🙂

Suggested posts for further reading


Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest