3

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.

enter image description here

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.

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35

3 Answers3

1

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

  • 1
    Not working at all on Android 11, it just changes dark mode with no transition whatsoever. – Shadow Apr 05 '21 at 07:06
0

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);
0

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);
            }
        }
    });```