Change color and text properties in an AlertDialog

1

I have a AlertDialog that shows a certain message that includes two variables, which are mail and pass and I want to be able to assign a color to it. Any ideas on how to do this?

I leave you my AlertDialog

        String correo=txtcorreo.getText().toString();
        String pass=txtpass.getText().toString();

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AddCuentaExpositor.this);
        alertDialogBuilder.setMessage("¿Esta seguro que desea crear la siguiente cuenta "+ correo + " con contraseña "+pass);

        alertDialogBuilder.setPositiveButton("Si",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        final String email=txtcorreo.getText().toString();
                        final String password=txtpass.getText().toString();

                        new Asyncinsert().execute(email,password);
                    }
                });

        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();



    }
    
asked by Ashley G. 17.01.2017 в 17:14
source

1 answer

3

There are several ways, but I think the simplest would be this, using the RGB color and applying a transformation of the html code, for example to change the color of the message text to red color ( #FF0000 ) would be made from this Form:

 alertDialogBuilder.setMessage(Html.fromHtml("<font color='#FF0000'>¿Esta seguro que desea crear la siguiente cuenta "+ correo + " con contraseña "+pass + "</font>"));

The same can be applied to the title.

To change the color only to the variables email and pass , it would be done this way:

 alertDialogBuilder.setMessage(Html.fromHtml("¿Esta seguro que desea crear la siguiente cuenta <font color='#FF0000'>"+ correo + "</font> con contraseña <font color='#FF0000'>"+pass + "</font>"));

You can actually use several types of html tags, for example if you want "Bold" text, simply use the containers <b> and </b> , text "italic" <i> and </i> , text larger by <big> and </big> etc ...:

I add a code example to document the comments in the answer:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        MainActivity.this);

// Configura el titulo.
alertDialogBuilder.setTitle(Html.fromHtml("<font color='#FF0000'>Author <b>Mihai Eminescu</b></font>"));

// Configura el mensaje.
    alertDialogBuilder
            .setMessage(Html.fromHtml("Por la noche, perezoso y cárdeno, arde el <font color='#FF0000'>fuego</font> en la chimenea;\n" +
                    "desde <u>un rincón en un sofá</u>  <font color='#FF0000'>rojo</font> yo lo miro de frente,\n" +
                    "<b>hasta que mi mente se duerme, hasta que mis pestañas se bajan</b>;\n" +
                    "la vela está apagada en la casa... <i>el sueño es</i> <font color='#0000FF'>cálido</font>, lento, suave.<br><hr>"))
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                MainActivity.this.finish();
            }
        })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        }).create().show();
    
answered by 17.01.2017 / 17:24
source