Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text in Alert Dialog Builder?

I'm trying to center some text in a default Alert Dialog Builder Here's my code so far, but it defaults to the left.

            new AlertDialog.Builder(getActivity())
                    .setTitle("Well done!")
                    .setMessage("Message centered????")

                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub

                                }
                            })

                    .setIcon(R.drawable.img_ok)
                    .show();

        }
like image 320
Violin Nz Avatar asked Jan 14 '14 20:01

Violin Nz


People also ask

How do I center the title of dialog alert in Android?

You cannot center the title in the default alert dialog. You will need to create a custom dialog in order to center the title.

What is the difference between an alert and an alert dialog?

Dialogs in Android are used to shows alerts for making decisions or to edit a single value. But there are some differences between an AlertDialog and a Dialog. In an AlertDialog you always want to show a message and at least one Button for user interaction.

What is AlertDialog message?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.


1 Answers

This piece of code does the job by default, without providing your own custom view to the AlertDialog.Builder .

  AlertDialog dialog = builder.show(); //builder is your just created builder
    TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    dialog.show();
like image 183
AlexAndro Avatar answered Nov 07 '22 07:11

AlexAndro