How to implement multi-language in application?

  • To implement multi-language in application, we have to create values folder for that languages we want to implement in application.

i.e :

Create values directories inside res directory.

1. For English :

values-en

  • strings.xml

<string name=“Please_Wait”>Please Wait</string>

2. For German :

values-zh

  • strings.xml

<string name=“Please_Wait”>Bitte warten</string>

 

3. For French :

values-fr

  • strings.xml

<string name=“Please_Wait”>Attendez sil vous plaít</string>

 

 How to integrate in application by Locale.

activity_main.xml

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

 

<Button

android:id=“@+id/btn_english”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“English” />

 

<Button

android:id=“@+id/btn_german”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“German” />

 

<Button

android:id=“@+id/btn_french”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“French” />

 

<TextView

android:id=“@+id/text”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_horizontal”

android:text=“Please Wait”

android:textAppearance=“?android:attr/textAppearanceLarge” />

</LinearLayout>

 

MainActivity.java

 

public class MainActivity extends Activity {

Button btn_english, btn_german, btn_french;

TextView text;

Configuration newConfig;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

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

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

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

btn_english.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

newConfig.locale = Locale.ENGLISH;

getResources().updateConfiguration(newConfig, null);

text.setText(getResources().getString(R.string.Please_Wait));

}

});

btn_german.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

newConfig.locale = Locale.GERMAN;

getResources().updateConfiguration(newConfig, null);

text.setText(getResources().getString(R.string.Please_Wait));

}

});

btn_french.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

newConfig.locale = Locale.FRENCH;

getResources().updateConfiguration(newConfig, null);

text.setText(getResources().getString(R.string.Please_Wait));

}

});

}