UgurGul
UgurGul

Reputation: 45

How do I checked the material toggle button when the application is opened?

I choose a theme using the toggle button.my goal is to have the theme_light toggle button selected when the app is first opened.I keep the state of the themes with SharedPreferencs because I use the recreate () function in the app. here are my codes;

   <com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/theme_toggle_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    app:selectionRequired="true"
    app:singleSelection="true"
    app:checkedButton="@+id/theme_light">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/theme_light"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="8dp"
        android:paddingEnd="12dp"
        android:minWidth="0dp"
        android:text="@string/light_theme"
        app:icon="@drawable/ic_brightness_7_black_24dp"
        app:iconPadding="4dp"
        android:onClick="theme__light"
        android:textColor="?attr/buttoncolor"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/theme_dark"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="8dp"
        android:paddingEnd="12dp"
        android:minWidth="0dp"
        android:text="@string/dark_theme"
        app:icon="@drawable/ic_brightness_4_black_24dp"
        app:iconPadding="4dp"
        android:onClick="theme__dark"
        android:textColor="?attr/buttoncolor"/>

</com.google.android.material.button.MaterialButtonToggleGroup>

For MainActivity;the following is in the onCreate function.

    sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
    checked=sharedPreferences.getInt("cek",0);

    if (checked==1){
    AppCompatDelegate.setDefaultNightMode(darktheme);

        materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
            group.check(R.id.theme_dark);
        });
    }
    else {
        AppCompatDelegate.setDefaultNightMode(defaultheme);
        materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
            group.check(R.id.theme_light);
        });

    }
   public void theme__dark(View view) {
    AppCompatDelegate.setDefaultNightMode(darktheme);
    checked=1;
    SharedCheckTheme();
}
public void theme__light(View view) {
    AppCompatDelegate.setDefaultNightMode(defaultheme);
    checked=2;
    SharedCheckTheme();
}
public void SharedCheckTheme(){
    SharedPreferences.Editor editor=sharedPreferences.edit();
    editor.putInt("cek",checked);
    editor.apply();
    recreate();
}

Upvotes: 1

Views: 1534

Answers (1)

froniq
froniq

Reputation: 105

Use the check(int ResId) method on the MaterialButtonToggleGroup:

MaterialButtonToggleGroup m = findViewById(R.id.theme_toggle_group);
m.check(R.id.theme_dark);

Upvotes: 3

Related Questions