Reputation: 11
i try to make a application with 2 theme, i can do this just for one activity and other activities are no longer affected, How do I change the theme to apply to all pages?
codes for themes:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#795548</item>
<item name="colorPrimaryDark">#5d4037</item>
<item name="colorAccent">#e6300b</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:navigationBarColor">#e4ccc7</item>
</style>
<style name="apptheme2" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#8ff2f2</item>
<item name="colorPrimaryDark">#044949</item>
<item name="colorAccent">#813ba4</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:navigationBarColor">#735852</item>
java codes for one activity:
Button btndark;
Button btnlight;
@Override
protected void onCreate(Bundle savedInstanceState) {
final SharedPreferences shared = getSharedPreferences("prefers", 0);
final Boolean theme = shared.getBoolean("theme", true);
if (!theme) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.apptheme2);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btndark = (Button) findViewById(R.id.btn1);
btnlight = (Button) findViewById(R.id.btn2);
btndark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = shared.edit();
editor.putBoolean("theme", false);
editor.commit();
recreate();
}
});
btnlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = shared.edit();
editor.putBoolean("theme", true);
editor.commit();
recreate();
}
});
Upvotes: 1
Views: 52
Reputation: 250
Extends below Activity in your all Activity. You can save integer in Shared preferences. So, you can know which theme is selected.
public class BaseActivity extends Activity {
public static int themePrimaryColor = R.color.ColorPrimary;
public static int themePrimaryColorDark = R.color.ColorPrimaryDark;
public static int themeAccentColor = R.color.colorAccent;
//for theme color
public static final int THEME_LIGHT = 1;
public static final int THEME_DARK = 2;
public static int currentTheme = R.style.AppTheme;
@Override
protected void onCreate(Bundle savedInstanceState) {
int themeColor = SettingPreference.getIntValue(SettingPreference.KEY_APP_THEME, THEME_LIGHT, this);
selectTheme(themeColor);
super.setTheme(currentTheme);
super.onCreate(savedInstanceState);
}
public static void selectTheme(int themeColor){
switch (themeColor) {
case THEME_LIGHT:
setCurrentTheme(R.style.AppTheme);
themePrimaryColor = R.color.ColorPrimary;
themePrimaryColorDark = R.color.ColorPrimaryDark;
themeAccentColor = R.color.colorAccent;
break;
case THEME_DARK:
setCurrentTheme(R.style.AppTheme2);
themePrimaryColor = R.color.color_primary2;
themePrimaryColorDark = R.color.color_primary_dark2;
themeAccentColor = R.color.color_accent2;
break;
default:
setCurrentTheme(R.style.AppTheme);
themePrimaryColor = R.color.ColorPrimary;
themePrimaryColorDark = R.color.ColorPrimaryDark;
themeAccentColor = R.color.colorAccent;
}
}
public static int getCurrentTheme() {
return currentTheme;
}
public static void setCurrentTheme(int currentTheme) {
BaseAppCompatActivity.currentTheme = currentTheme;
}
}
After restart your application, your application theme is changed as per your selection.
Upvotes: 0
Reputation: 3347
try this code: write this code in your manifest
<activity
android:name="Activity1"
android:theme="@style/AppTheme1" />
for another activity
<activity
android:name="Activity2"
android:theme="@style/AppTheme2" />
Upvotes: 1