Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font size for an AlertDialog message [duplicate]

I'm trying to change the font size of the AlertDialog message.

Button submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                Application1GoodExample.this);
        builder.setMessage("Your form has been successfully submitted");
        TextView textView = (TextView) findViewById(android.R.id.message);
        textView.setTextSize(40);
        builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();             
            }
        });

        builder.show();
    }
});

I get an error message saying that "findViewById()" is undefined for the type AlertDialog.Builder.

like image 747
Alex Avatar asked Mar 12 '13 13:03

Alex


3 Answers

Use this :

AlertDialog alert = builder.create();
alert.show();

TextView msgTxt = (TextView) alert.findViewById(android.R.id.message);   
msgTxt.setTextSize(16.0);

in your case :

Button submit = (Button) findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);

        builder.setMessage("Your form has been successfully submitted");
        builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();             
            }
        });

        // this will solve your error
        AlertDialog alert = builder.create();
        alert.show();
        alert.getWindow().getAttributes();

        TextView textView = (TextView) alert.findViewById(android.R.id.message);
        textView.setTextSize(40);
        Button btn1 = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        btn1.setTextSize(16);
    }
});

If this didn't help you, post your LogCat Error in your question.

like image 72
AwadKab Avatar answered Nov 20 '22 20:11

AwadKab


Try this

 AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
 TextView textView = (TextView) dialog.findViewById(android.R.id.message);
 textView.setTextSize(40);
like image 32
R KiranKumar Avatar answered Nov 20 '22 20:11

R KiranKumar


the method findViewById belongs to the type View.

AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.cancel();           
    } 
});
builder.setMessage("Your form has been successfully submitted");
AlertDialog theDialog=builder.create();
TextView textView = (TextView) theDialog.findViewById(android.R.id.message);
textView.setTextSize(40);

theDialog.show();
like image 30
Sebastian Breit Avatar answered Nov 20 '22 20:11

Sebastian Breit