3

I have a 4 line text in my alertDialog and I want to align it centrally without providing any custom layout.Is it possible.

James
  • 13,891
  • 26
  • 68
  • 93
  • 1
    Yes of course it is possible. But what do you mean by *without providing any custom layout*? – Phil Oct 19 '11 at 04:46
  • I think he's refering to the android documentation that says "If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it" – Kurtis Nusbaum Oct 19 '11 at 04:55
  • My Question is for the default AlertView where the message appears. It is by default left alignde. Can we make it center aligned. – James Oct 19 '11 at 16:41

1 Answers1

7

You can do it. Center align a standard AlertView dialog, by setting the gravity of the message text element of the standard dialog, in your code, as follows:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
// the following line gets a handler to the message element inside the dialog.
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
// and now all that is needed is changing the gravity ...
messageText.setGravity(Gravity.CENTER);
wiztrail
  • 3,988
  • 2
  • 19
  • 18
  • can we get the handler to the title also like this. – James Jan 29 '12 at 07:28
  • As far as i know the answer is NO. If you really need to align also the title, the solution is to extend the AlertDialog with a new XML theme, that would format the new dialog according to the theme attributes. for more info refer to [Right justify text in AlertDialog](http://stackoverflow.com/a/6131224/978329) – wiztrail Jan 29 '12 at 14:08