Home > Android > Multilanguage in android app

Multilanguage in android app


First we must create these files: main.xml, MainActivity.java, values-en/string.xml, values-vi/string.xml

Content of these files was created below:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:editable="true"
        android:id="@+id/txt_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btn_change1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VietNam" />

    <Button
        android:id="@+id/btn_change2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="English" />

</LinearLayout>

MainActivity.java

import java.util.Locale;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
	Resources res;
	Button btn1,btn2;
	TextView txt;
	String language;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ChangeLang("en");
    }

    public void ChangeLang(String lang)
    {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        setContentView(R.layout.main); 
        btn1 = (Button) findViewById(R.id.btn_change1);
        btn2 = (Button) findViewById(R.id.btn_change2);       
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    } 
	@Override
	public void onClick(View v) 
	{
		if(v == btn1)
		{
			ChangeLang("vi");
			Toast.makeText(getBaseContext(), "1", Toast.LENGTH_SHORT).show();
		}
		if(v == btn2)
		{	
			ChangeLang("en");
			Toast.makeText(getBaseContext(), "2", Toast.LENGTH_SHORT).show();
		}
	}
}

values-en/string.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<resources> <string name=”app_name”>My First Demo</string>

<string name=”test”>This is English</string> </resources>

values-vi/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">My First Demo</string>
	<string name="test">Đây là Tiếng Việt</string>
</resources>

You can download full demo at this link https://dl.dropbox.com/u/82707883/multiResource.rar

Source: http://vietandroid.com/threads/multilanguage-da-ngon-ngu.9523/

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: