11

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.

Alex
  • 876
  • 6
  • 17
  • 38

4 Answers4

29

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.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
AwadKab
  • 3,054
  • 2
  • 17
  • 17
4

Try this

 AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
 TextView textView = (TextView) dialog.findViewById(android.R.id.message);
 textView.setTextSize(40);
R KiranKumar
  • 825
  • 1
  • 8
  • 27
1

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();
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
  • For some reason my progam crashes when I press the submit button to display an alert dialog message – Alex Mar 12 '13 at 13:21
  • maybe if you show us the logcat output :) – Sebastian Breit Mar 12 '13 at 15:52
  • I have used AwadKab's solution and managed to change the size of the Alert message without getting any errors. Do you know if I can do the same for the "Exit" button? I'm very new to Android development and cannot figure out how to do this. thank you. – Alex Mar 12 '13 at 16:00
  • 1
    if you want to customize the elements of a dialog, the best way is to create a brand new view for your dialog. Take a look at this: http://www.mkyong.com/android/android-custom-dialog-example/ – Sebastian Breit Mar 12 '13 at 16:12
0

Since you are not using CustomDialog, i suggest you to add TextView as following,

TextView textView = (TextView) findViewById(android.R.id.message); // remove builder object
Raynold
  • 443
  • 2
  • 9
  • 28
  • This worked, thank you. However, my program crashes when I press the Submit button. I've updated my code above. – Alex Mar 12 '13 at 13:16