I know it might be easy but is just something that I don't really know. I am new at coding and I tried different codes in my app.
The code that I currently used does nothing:
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_YES:
case Configuration.UI_MODE_NIGHT_NO:
break;
}
}
}
});
I don't know if this is the one that is meant to do its thing or not. I just want a code that should solve my problem.
This is the night theme in my values:
<style name="Theme.Diligent" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryVariant">@color/black</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/black</item>
<item name="colorSecondaryVariant">@color/black</item>
<item name="colorOnSecondary">@color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
Thank you in advance.
To enable night mode use
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
and to disable it useAppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
.