7

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
  • 277
  • 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

4 Answers4

14

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,896
  • 1
  • 27
  • 53
5

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

Gil Vegliach
  • 3,472
  • 2
  • 23
  • 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,590
  • 4
  • 23
  • 49
Farido mastr
  • 393
  • 3
  • 11