Change the background color of a pop-up dialog
Credit goes to Sushil
Create your AlertDialog as usual:
AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
Dialog dialog = dialog.create();
dialog.show();
After calling show() on your dialog, set the background color like this:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
If you just want a light theme and aren't particular about the specific color, then you can pass a theme id to the AlertDialog.Builder constructor.
AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT)...
or
AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)...
To expand on @DaneWhite's answer, you don't have to rely on the built-in themes. You can easily supply your own style:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>
</style>
and then apply it in the Builder constructor:
Java:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
...
.create();
Kotlin:
var alertDialog = AlertDialog.Builder(context, R.style.MyDialogTheme)
...
.create()
This should work whether you are using android.support.v7.app.AlertDialog
or android.app.AlertDialog
This also works better than @DummyData's answer because you don't resize the dialog. If you set window's background drawable you overwrite some existing dimensional information and get a dialog that is not standard width.
If you set background on theme and the set the theme on dialog you'll end up with a dialog that is colored how you want but still the correct width.