1

I have created an Alert Dialog, and i want to know how to set the gravity of the message as center:

Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(completedMessage());
dialog = builder.create();

here is the completedMessage() method:

private String completedMessage() {
  String[] messages = getResources().getStringArray(R.array.success);
  String completeMsg =  "\n";
  int messagei= (int) Math.floor(Math.random() * messages.length); 
  completeMsg += messages[messagei];

  return completeMsg;

}

and here is the arrays.success:

<string-array name="success">
    <item>You got it!</item>
    <item>Success!</item>
    <item>Done!</item>
    <item>Great job, young Padawan.</item>
</string-array>

no title, no buttons, no list, just the message, and i want to show it centered, no matter the string length.

I have tried:

Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(completedMessage());
dialog = builder.show();
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER_VERTICAL);

But this changes nothing.

Btw, the message shows in the box like having a padding on top and left, but no padding on the bottom nor right.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
iversoncru
  • 579
  • 8
  • 22

1 Answers1

2

try this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage("your message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
Aspicas
  • 4,498
  • 4
  • 30
  • 53