如何根据用户选择单击单选按钮来更改所有活动的应用程序主题,标题栏颜色和背景颜色
答案 0 :(得分:0)
//Pseudo code snippet is given
Let me assume the first page of android app might be having different set of radio as blue,green, red etc.,
each color might have defined in the style.xml with the required background,foreground,font color etc.,
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/radio_blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pirates" android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ninjas" android:onClick="onRadioButtonClicked"/>
</RadioGroup>
//In utility method
public static void savePreferences(String key, String value, Context context) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
//in that page radiobuttononchange
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.blue:
if (checked)
//store the value in sharedpreferences
savePreferences("preference","blue",getcontext());
break;
case R.id.red:
if (checked)
savePreferences("preference","red",getcontext());
break;
Intent nextScreen = new Intent(
context,
Form.class);
startActivity(nextScreen);
}
}
//In next activity
//Oncreate
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String themeName = pref.getString("preference", "Theme1");
if (themeName.equals("blue")) {
setTheme(R.style.blue);
} else if (themeName.equals("red")) {
setTheme(R.style.red);
}.....
setContentView(R.layout.activity_some_layout);
}