Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the background color of a pop-up dialog

I wrote android code that shows a pop-up dialog but I want to change the background color from black to white , and then the color of the writing.

This is the dialog's code:

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);      Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);      if (!welcomeScreenShown) {           String whatsNewText = getResources().getString(R.string.Text);         new AlertDialog.Builder(this).setMessage(whatsNewText).setPositiveButton(                 R.string.ok, new DialogInterface.OnClickListener(){             public void onClick(DialogInterface dialog, int which) {                 dialog.dismiss();             }         }).show();         SharedPreferences.Editor editor = mPrefs.edit();         editor.putBoolean(welcomeScreenShownPref, true);         editor.commit(); // Very important to save the preference     } 
like image 273
Rick Avatar asked Aug 21 '13 00:08

Rick


People also ask

How do I change the background color of alert in dialog?

You should use the android:windowBackground theme attribute instead. android:background will change the background color of every View . Actually, both background and windowBackground remove rounded corners from the dialog window. If you want to set a color, you should use colorBackground attribute instead.

How do I customize my dialog box?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What will be the background Colour of the following alert dialog snippet?

on your dialog builder. It will force the background to white color (instead of dark grey) on android version before Froyo.


2 Answers

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.

like image 187
tir38 Avatar answered Sep 19 '22 12:09

tir38


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)... 
like image 29
Dane White Avatar answered Sep 19 '22 12:09

Dane White