1

How do I change the color/s in my styles.xml which looks like this

<?xml version="1.0" encoding="utf-8"?>

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">#FF9800</item>
    <item name="colorButtonNormal">#FF9800</item>
    <item name="android:colorPrimaryDark">#FF9800</item>
    <item name="android:navigationBarColor">#FF9800</item>
</style>

when I toggle my ToggleButton in my activity_main.xml, which looks like this

    <ToggleButton
    android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Light Theme"
    android:textOff="Dark Theme"
    android:onClick="onToggleClicked"
    android:layout_above="@+id/button"
    android:layout_centerHorizontal="true" />

so far I have done this to make that behavior happen in my MainActivity.java

    public void onToggleClicked(View view) {
    // check if toggle active
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {

    } else {

    }
}

The reason is that I want to make a dark and a light theme for my app:)

utdev
  • 17
  • 8

2 Answers2

1

You cannot dynamically change the appTheme. But there is a workaround.

Check out this link.

Android - Change app Theme on onClick

Community
  • 1
  • 1
Zahid
  • 677
  • 4
  • 6
0

Themes are, unfortuantly, immutable, meaning that you can't change them programatically during runtime.

Nor can you simply set a style. You can, however, change the style by inflating a new view. You may find this thread useful.

Community
  • 1
  • 1
Alex K
  • 8,269
  • 9
  • 39
  • 57