1

I'm using an AlertDialog to show the result of a game. My problem is that the dialog seems to changes the depiction of my normal activiymain layout while the dialog is open.

That's the layout without the dialog

enter image description here

That's the layout while the dialog is open

enter image description here

As you can see the Textviews that i use as buttons are colored weirdly. My Dialog is created like this:

AlertDialog.Builder = new AlertDialog.Builder(MainActivity.this);
turnout.setTitle("Dialog");

The mainactivity layout is a relative layout and the textviews are colored in #ffffff (meaning white)

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
TimWalter
  • 47
  • 1
  • 12
  • I don't really see any difference, but this little "grey shadow" on the background is something Android use to make the popup more evident, if you don't want it, using a fragment popped up as a dialog might be the solution – Pier Giorgio Misley Nov 16 '16 at 10:16
  • Or check this [SO post](http://stackoverflow.com/questions/15007272/how-to-change-the-background-color-around-a-dialogfragment) and set the color for the area around the dialog according to your requirement. – Bö macht Blau Nov 16 '16 at 10:20

1 Answers1

0

By default Android dims the background (displays the "grey shadow" behind the dialog) whenever a dialog is displayed. Changing this default behavior may decrease the readability of a dialog and confuse a user.

That being said, you can disable the background by setting the parameter android:backgroundDimEnabled to false in your dialog's style.

You add the theme to your styles.xml:

<style name="NoDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="android:backgroundDimEnabled">false</item>
</style>

Make sure to inherit the default AlertDialog's style.

Additionally you can control the transparency of the dim, using android:backgroundDimAmount. This parameter takes a value from 0 (no dim) to 1 (background completely black). By default Android seems to use 0.6.

<style name="LessDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="android:backgroundDimAmount">0.25</item>
</style>

Next, pass the style name to theAlertDialog.Builder's constructor in your MainActivity.java:

new AlertDialog.Builder(this, R.style.NoDimAlertDialog)
    .setTitle("Some title")
    .setMessage("Some message")
    .show();

You may as well consider using a Fragment to display your AlertDialog. Android comes with handy DialogFragment class for that scenario. A simple DialogFragment in your case may look like that:

class MyDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getContext(), R.style.NoDimDialog)
            .setTitle("Some title")
            .setMessage("Some message")
            .create();
    }
}

Then you add your Fragment in MainActivity.java:

new MyDialogFragment().show(getSupportFragmentManager(), "DialogTag");
Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19