Reputation: 127
I have a ListView
and multiple layouts , all the layouts are set when I click on ListView
item, every item click contains different layout and I have two languages selected by radio button on one layout. After that when I clicked on item string gets refreshed but the ImageButton
is not refreshed with different language. I have already an image with both languages and all my layouts are saved in an array.
My code is the following:
When i clicked on save button after language selection:
if (lang_selected.equalsIgnoreCase("English"))
{
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
//initView(view);
}else if(lang_selected.equalsIgnoreCase("Chinese"))
{
Locale locale = new Locale("zh");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
//initView(view);
}
When i clicked on listview item:
protected void onGeneralInfoItemClick(AdapterView<?> parent,
final int position) {
final GeneralInfoData obj = (GeneralInfoData) parent
.getItemAtPosition(position);
final View layoutView = obj.getDataLayoutView();
detailCantiner.removeAllViews();
detailCantiner.addView(layoutView);
try{
if(position==0)
{
tvTradeName.setText(R.string.trade);
btnSaveTrade.setImageResource(R.drawable.general_info_save_button);
String
str=Util.getSharedPreference(context).getString("UserCompany", "");
trade_name.setText(str);
}if(position==1).......................**
Upvotes: 3
Views: 2258
Reputation: 235
You can use following code, using these functions wherever applicable in your code.
For language change....
public void changeLang(String lang) {
if (lang.equalsIgnoreCase(""))
return;
iocLocale = new Locale(lang);
Log.v("My Language",iocLocale + "");
saveLocale(lang);
Locale.setDefault(iocLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = iocLocale;
getResources().updateConfiguration(config,
getResources().getDisplayMetrics());
}
For refreshing text....
private void refreshTextchange()
{
txt_home.setText(getResources().getString(R.string.home));
txt_patient.setText(getResources().getString(R.string.Patientt));
txt_staff.setText(getResources().getString(R.string.Stafff));
txt_makechange.setText(getResources().getString(R.string.MakeChangeAppointment));
txt_map.setText(getResources().getString(R.string.Map));
txt_contact.setText(getResources().getString(R.string.Contact));
}
For saving the language....
public void saveLocale(String lang) {
SharedPreferences prefs = getSharedPreferences("com.ioc",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("language", lang);
editor.commit();
}
For loading language i.e., first time you run your code...
public void loadLocale() {
SharedPreferences prefs = getSharedPreferences("com.ioc",
Activity.MODE_PRIVATE);
String language = prefs.getString("language", "");
changeLang(language);
iocLocale = new Locale(language);
Log.v("My Language",language + "");
Log.d("c", "333333333");
}
For checking language...
private boolean checkLocal()
{
SharedPreferences prefs = getSharedPreferences("com.ioc",
Activity.MODE_PRIVATE);
String language = prefs.getString("language", "");
if(language.equalsIgnoreCase("pt"))
return true;
else
return false;
}
Upvotes: 3
Reputation: 5260
try to do like this
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main);
setTitle(R.string.app_name);
// Checks the active language
if (newConfig.locale == Locale.ENGLISH) {
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
} else if (newConfig.locale == Locale.FRENCH){
Toast.makeText(this, "French", Toast.LENGTH_SHORT).show();
}
}
and use it like as follows
Configuration newConfig = new Configuration();
newConfig.locale = Locale.FRENCH;
onConfigurationChanged(newConfig);
Upvotes: 1