Reputation: 3923
I have a strange problem. When I run app and click button (btn_eng or btn_pl) i see log in logcat ("Erase dialog" or "Erase dialog PL") but next time when i click it does't show up and button doesn't do their onClick action. I tried to make buttons to change language. In my app I have a dialog with data form Resources (string array) and when I change language i want erase dialog and make new with adequate data.
//public static Activity act; -> before onCreate(..)
this.act = this;
Button btn_eng = (Button) findViewById(R.id.btnEN);
btn_eng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog");
act.removeDialog(1);
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
act.setContentView(R.layout.main);
}
});
Button btn_pl = (Button) findViewById(R.id.btnPL);
btn_pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog PL");
act.removeDialog(1);
Locale locale = new Locale("pl");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
act.setContentView(R.layout.main);
}
});
Upvotes: 4
Views: 2176
Reputation: 1393
try to remove setcontentView() call in each Onclicklistener and try to put below code and test
Button btn_eng = (Button) findViewById(R.id.btnEN);
btn_eng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog");
act.removeDialog(1);
if(v.getId()==R.id.btnEN){
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
});
Button btn_pl = (Button) findViewById(R.id.btnPL);
btn_pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog PL");
act.removeDialog(1);
if(v.getId()==R.id.btnPL){
Locale locale = new Locale("pl");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
});
Please share the result and accept this if it works at your end as its working well at my end
Upvotes: 0
Reputation: 27549
setContentView(R.layout.main);
calling more then one time is not allowed, If you want change your screen contents, You should have Layouts/Views/ViewGroups in your Layout/.xml file and update them instead of calling setContentView Again.
Upvotes: 0
Reputation: 1498
Its because you are resetting the content view in your onClick. Calling setContentView
does not cause onCreate
to be run - and that is where (I'm guessing) you link the onClick
listener to the button. It does however cause a whole new instance of your layout to be displayed. You need to find a new way to refresh your screen, not replace your screen, with your new data. The best way would be to grab the wrapping ViewGroup
for you activity and call View#invalidate();
Upvotes: 3