How do I change the color of AlertDialog

1

I put in my app where you can show the dialog to exit the application, but the dialog is black. I wanted him to go white. How do I change the AlertDialog theme?

@Override // SAIR DA APLICAÇÃO
public void finish() {

    if (sair) {

        AlertDialog.Builder alerta = new AlertDialog.Builder(this);


        alerta.setTitle("Sair");

        alerta.setIcon(R.mipmap.ic_launcher);
        alerta.setMessage("Tem certeza de que deseja sair do xxxxx")
                .setCancelable(false)
                .setPositiveButton("SIM",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                sair = false;
                                finish();

                            }
                        })
                .setNegativeButton("NÃO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alertDialog = alerta.create();
        alertDialog.show();
    } else {
        super.finish();
    }

}
    
asked by anonymous 17.11.2016 / 05:42

2 answers

5

To change the properties of a AlertDialog , you must add the style to the constructor of the dialog box:

alerta = new AlertDialog.Builder(this, R.style.DialogStyle);

In this way, you can create the configuration you want within DialogStyle .

<style name="DialogStyle" parent="@android:style/Theme.Material.Light.Dialog.Alert">
        <item name="android:colorAccent">@color/material_blue_700</item>
</style>

Please note that use of this builder is available only at the API 11+ level.

    
17.11.2016 / 12:20
1

I've already got it, just insert a line of code on it needed to show the white AlertDialog, say the Background Theme.

Thanks,

@Override // SAIR DA APLICAÇÃO
public void finish() { 
    if (sair)
    {
        //trecho modificado 
        AlertDialog.Builder alerta = new AlertDialog.Builder(this,
                android.R.style.Theme_Material_Light_Dialog_Alert);
    }
}
    
17.11.2016 / 16:40