Question

I want to change the background of the standard Holo.Light dialog box. When I set my custom view to the dialog alert it just grows in height.

Here is my code for a normal dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mLogin);
alertDialogBuilder.setTitle("No Connection");
alertDialogBuilder.setMessage("You are not connected to a Wifi or data connection. Please connect and retry");
alertDialogBuilder.setNeutralButton(getString(R.string.miscClose), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }

});
alertDialogBuilder.show();

This code displays this dialog

enter image description here

I then try to add my view like so:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mLogin);
alertDialogBuilder.setView(View.inflate(mLogin, R.layout.test_alert, null));
alertDialogBuilder.setTitle("No Connection");
alertDialogBuilder.setMessage("You are not connected to a Wifi or data connection. Please connect and retry");

alertDialogBuilder.setNeutralButton(getString(R.string.miscClose), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});
alertDialogBuilder.show();

This code displays this dialog:

enter image description here

I don't understand why It is just adding that space. I added a simple layout which background color is blue. Shown below:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/YakBlue">


</LinearLayout>

Any help on how to change a basic alert dialog background colour to blue would be great.

P.S I have tried themes but to no success. My min apk is 9.

Thanks

Was it helpful?

Solution

that's because the alert dialog's layout contains a container that puts your view inside it, so what you do is just adding an empty view that takes space. try to change its color to red, and you will notice it's just below the text and above the buttons.

in order to change the style of the dialog, you should, well change its style. here's a nice library that demonstrates how to do it, using the basic holo style. you can also use the one that is shown on HoloEverywhere library.

of course, if you don't want to support old devices (but according to the minSdk, you do), you can directly change the style and set a theme for the app itself, which will change the dialogs style on the entire app.

Not only that, but you don't even have to use AlertDialog. you can create your own dialogs instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top