diff options
-rw-r--r-- | appcompat/src/androidTest/java/androidx/appcompat/app/AlertDialogTest.java | 24 | ||||
-rw-r--r-- | appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java | 29 |
2 files changed, 39 insertions, 14 deletions
diff --git a/appcompat/src/androidTest/java/androidx/appcompat/app/AlertDialogTest.java b/appcompat/src/androidTest/java/androidx/appcompat/app/AlertDialogTest.java index ab34aa4fbbf..c600663344e 100644 --- a/appcompat/src/androidTest/java/androidx/appcompat/app/AlertDialogTest.java +++ b/appcompat/src/androidTest/java/androidx/appcompat/app/AlertDialogTest.java @@ -39,6 +39,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.any; @@ -49,6 +50,7 @@ import static org.mockito.Mockito.verify; import android.content.Context; import android.content.DialogInterface; +import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.graphics.drawable.ColorDrawable; @@ -58,6 +60,7 @@ import android.text.TextUtils; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; +import android.view.Window; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckedTextView; @@ -1349,6 +1352,27 @@ public class AlertDialogTest { AlertDialog.BUTTON_POSITIVE); } + @Test + @UiThreadTest + public void testBackgroundDrawable() throws Throwable { + final AlertDialog dialog = new AlertDialog.Builder(mActivityTestRule.getActivity()) + .setTitle(R.string.alert_dialog_title) + .setMessage(R.string.alert_dialog_content) + .create(); + + // Now set the windowBackground of the Dialog + final ColorDrawable background = new ColorDrawable(Color.MAGENTA); + final Window window = dialog.getWindow(); + final View decorView = window.getDecorView(); + window.setBackgroundDrawable(background); + + // Show the Dialog + dialog.show(); + + // And assert that the background is maintained + assertSame(background, decorView.getBackground()); + } + private static class TestDrawable extends ColorDrawable { private int mWidth; private int mHeight; diff --git a/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java b/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java index 06ca9727a80..e761e03070c 100644 --- a/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java +++ b/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java @@ -283,10 +283,6 @@ class AppCompatDelegateImpl extends AppCompatDelegate mAppCompatCallback = callback; mHost = host; - if (window != null) { - attachToWindow(window); - } - if (mLocalNightMode == MODE_NIGHT_UNSPECIFIED && mHost instanceof Dialog) { final AppCompatActivity activity = tryUnwrapContext(); if (activity != null) { @@ -308,6 +304,10 @@ class AppCompatDelegateImpl extends AppCompatDelegate } } + if (window != null) { + attachToWindow(window); + } + // Preload appcompat-specific handling of drawables that should be handled in a special // way (for tinting etc). After the following line completes, calls from AppCompatResources // to ResourceManagerInternal (in appcompat-resources) will handle those internal drawable @@ -327,6 +327,8 @@ class AppCompatDelegateImpl extends AppCompatDelegate // Dialogs, etc mBaseContextAttached = true; + applyDayNight(); + // We lazily fetch the Window for Activities, to allow DayNight to apply in // attachBaseContext ensureWindow(); @@ -349,16 +351,6 @@ class AppCompatDelegateImpl extends AppCompatDelegate } } - applyDayNight(); - - final TintTypedArray a = TintTypedArray.obtainStyledAttributes( - mContext, null, sWindowBackgroundStyleable); - final Drawable winBg = a.getDrawableIfKnown(0); - if (winBg != null) { - mWindow.setBackgroundDrawable(winBg); - } - a.recycle(); - mCreated = true; } @@ -626,6 +618,15 @@ class AppCompatDelegateImpl extends AppCompatDelegate // Now install the new callback window.setCallback(mAppCompatWindowCallback); + final TintTypedArray a = TintTypedArray.obtainStyledAttributes( + mContext, null, sWindowBackgroundStyleable); + final Drawable winBg = a.getDrawableIfKnown(0); + if (winBg != null) { + // Now set the background drawable + window.setBackgroundDrawable(winBg); + } + a.recycle(); + mWindow = window; } |