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);
}
});
Its because you are resetting the content view in your onClick. Calling
setContentViewdoes not causeonCreateto be run – and that is where (I’m guessing) you link theonClicklistener 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 wrappingViewGroupfor you activity and call View#invalidate();