How do I change the color of Alertdialog

Asked

Viewed 1,767 times

1

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

@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();
    }

}
  • On which Android are you testing? Also, show a screenshot with this dialog.

  • Plabo already managed, just insert a line of code in what needed to show the white Alertdialog, say the background theme. Thanks, @Override // QUIT APPLICATION public void Finish() { if (quit) { modified chunk Alertdialog.Builder alerta = new Alertdialog.Builder(this, android.R.style.Theme_material_light_dialog_alert);

  • 1

    @Anselmocardoso if you want you can answer your own question.

2 answers

6


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

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

This way you can create the configuration you want inside the DialogStyle.

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

Note that the use of this constructor is only available at the level API 11+.

2

already managed, just insert a line of code in what needed to show the white Alertdialog, I say the background theme.

Thank you,

@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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.