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();
}
You cannot center the title in the default alert dialog. You will need to create a custom dialog in order to center the title.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With