I want the message text within my dialog box to be center aligned.
You cannot center the title in the default alert dialog. You will need to create a custom dialog in order to center the title.
There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)
Of course, you can always set the gravity of the original text view. This allows you to not have to worry about formatting and padding.
For example
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title"); builder.setMessage("Message"); builder.setPositiveButton("OK", null); AlertDialog dialog = builder.show(); // Must call show() prior to fetching text view TextView messageView = (TextView)dialog.findViewById(android.R.id.message); messageView.setGravity(Gravity.CENTER);
Create your own TextView object and then supply it to popup builder as View:
AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this); TextView myMsg = new TextView(this); myMsg.setText("Central"); myMsg.setGravity(Gravity.CENTER_HORIZONTAL); popupBuilder.setView(myMsg);
You can control all other text parameters (style, color, size ...). To control margins you may programatically create LinearLayout, set LayoutParams, and then put TextView into it.
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