LocalActivity.java
package com.local;
import java.util.Locale;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocalActivity extends Activity {
/** Called when the activity is first created. */
Button mButton;
TextView mTextView;
String language ="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton=(Button)findViewById(R.id.button);
mTextView=(TextView)findViewById(R.id.txt);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Locale locale = null;
if (language.equalsIgnoreCase("en")) {
locale = new Locale("en");
} else if (language.equalsIgnoreCase("es")) {
locale = new Locale("es");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
mTextView.setText(getString(R.string.app_name));
// mButton.setText(getString(R.string.app_name));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu , menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.english:
language="en";
break;
case R.id.spanish:
language="es";
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
In values folder string.xml
string.xml files
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Application Name</string>
</resources>
-----------------------------------------------------------------
in values-es folder string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LocalActivity!</string>
<string name="app_name">nombre de la aplicaciĆ³n</string>
</resources>
in Device select menu option for change language.
package com.local;
import java.util.Locale;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocalActivity extends Activity {
/** Called when the activity is first created. */
Button mButton;
TextView mTextView;
String language ="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton=(Button)findViewById(R.id.button);
mTextView=(TextView)findViewById(R.id.txt);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Locale locale = null;
if (language.equalsIgnoreCase("en")) {
locale = new Locale("en");
} else if (language.equalsIgnoreCase("es")) {
locale = new Locale("es");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
mTextView.setText(getString(R.string.app_name));
// mButton.setText(getString(R.string.app_name));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu , menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.english:
language="en";
break;
case R.id.spanish:
language="es";
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
In values folder string.xml
string.xml files
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Application Name</string>
</resources>
-----------------------------------------------------------------
in values-es folder string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LocalActivity!</string>
<string name="app_name">nombre de la aplicaciĆ³n</string>
</resources>
in Device select menu option for change language.