9

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

        }
Violin Nz
  • 297
  • 1
  • 5
  • 15
  • I have found `AlertDialog` to be hard to customize. I ended up creating a custom `Dialog` that contains a `View` exactly how I want. – codeMagic Jan 14 '14 at 20:49
  • I think you can find you answer here : http://stackoverflow.com/questions/4954130/message-text-in-android-dialog-box I hope it help you ! – koni Jan 14 '14 at 20:51
  • @koni Yes, I saw that link, quick answer. – Violin Nz Jan 14 '14 at 21:02

5 Answers5

15

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();
AlexAndro
  • 1,918
  • 2
  • 27
  • 53
  • 1
    setView is missing, dialod.setView(messageText); should be included before dialog.show(); – iversoncru Mar 11 '15 at 10:29
  • FYI, the solution will only work if the "dialog.show()" is executed BEFORE the ".setGravity()", otherwise you'll get a NulPtr runtime exception – Oke Uwechue Sep 04 '22 at 17:44
6

Instead of setting the message, use AlertDialog.Builder.setView(View) with a View with centred text

Gil Vegliach
  • 3,542
  • 2
  • 25
  • 37
1

you can try this code

public void Info(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Info Aplikasi");
    builder.setIcon(R.drawable.info_adp);
    builder.setMessage("Jakarta Hospital");
    builder.setCancelable(false);
    builder.setPositiveButton("Exit", null);
    AlertDialog dialog = builder.show();
    TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
    messageView.setGravity(Gravity.CENTER);
}
0

You can try this simple method:

textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
textView.setGravity(Gravity.CENTER);
alert.setView(textView);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Farido mastr
  • 464
  • 5
  • 12
0

For Material Alert Dialog would be like this:

MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
    .setTitle("Titulo")
    .setIcon(R.drawable.people)
    .setMessage("un mensaje cualquiera");
AlertDialog dialog = builder.show();    
((TextView) dialog.findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
Angel
  • 190
  • 2
  • 10