mercredi 8 juillet 2015

Android java switch to different xml or change style

How can I change my style.xml (give different colors) or refer to a different style.xml if an event occurs.

In this case the event is a toggle button.

The ToggleButton in my activity_main.xml 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" />

my styles.xml look 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>

and my MainActivity.java like this

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

    if (on) {

    } else {

    }
}

Aucun commentaire:

Enregistrer un commentaire