Reputation: 2511
We implemented night mode to our application. it works like a charm except for its transition. We are using the Base Application class to implement it. The problem is no matter what we tried we couldn't achieve a smooth transition when the configuration changes.
We tried to implement enter and exit animation in our style. But it applies to the whole activity. So it also affects others transitions of the activity. So it didn't work.
As can be seen from the image there is a black blinking appear on the screen when the configuration changes.
Configuration Change Code :
public static void applyTheme(@NonNull String themePref) {
switch (themePref) {
case LIGHT_MODE: {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Log.d(Statics.LOG_TAG, "Applying day mode");
break;
}
case DARK_MODE: {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Log.d(Statics.LOG_TAG, "Applying night mode");
break;
}
default: {
Log.d(Statics.LOG_TAG, "Applying automatic mode");
if (BuildCompat.isAtLeastP()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
}
break;
}
}
}
Thanks for reading this. Any help is appreciated.
Upvotes: 3
Views: 1709
Reputation: 11
day_night=findViewById(R.id.day_night);
day_night.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
});```
Upvotes: 0
Reputation: 190
Add the following code after setting the theme, works for me
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
Upvotes: 0
Reputation: 377
Please use the following code it is working perfect -
//Does not work in Android Nugget
public void setDayNightMode(boolean day) {
if (day)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
recreate();
}
//Style
<style name="WindowAnimationFadeInOut">
<item name="@android:windowEnterAnimation">@anim/fade_in</item>
<item name="@android:windowExitAnimation">@anim/fade_out</item>
</style>
// fade in inside anim folder
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toAlpha="1.0" />
</set>
// fade out inside anim folder
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toAlpha="0" />
</set>
I tested it it is working
Upvotes: 1