I want the message text within my dialog box to be center aligned.
8 Answers
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);
- 11,121
- 8
- 41
- 39
-
1Awesome trick. But it doesn't work for 'title' of AlertDialog ! – Gaurav Arora Nov 01 '12 at 12:48
-
I've not tested, but I'm sure you could do a similar thing for the Title by doing android.R.id.title. This works great for the message though. – Joss Stuart Nov 05 '12 at 13:59
-
Do you think this has a better perfomance? I know that this is impossiible to get from the user, just curious. – Enrichman May 17 '13 at 13:56
-
This is also a great solution as I can set a separate view to my dialog and still be able to control the attributes of the TextView. A custom TextView forces me to use it as the dialog's view. – sturrockad Nov 06 '13 at 10:30
-
5For those who get null as a result. Read the answer carefully. _Must call show() prior to fetching text view_ – Dominic Aug 20 '14 at 12:10
-
1Just a little FYI if anybody come across this, **To change the text style of Positive,Negative and Neutral buttons the ids are `button1`,`button2` and `button3` respectively** – IronBlossom Feb 09 '15 at 10:56
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.
- 10,918
- 5
- 47
- 45
-
1In case I want to add buttons or any other default components in order to get the default look of the dialog I need to do it by myself. I liked more Chase answer. – Alex Kapustian Jun 09 '12 at 12:14
-
I have to admit that I also like more the answer from Chase. This was the first approach that came to my mind, and I wanted to answer it quickly. – Zelimir Mar 25 '13 at 09:09
Building off of Chase's answer, here's how to also center the title. I think this is the easiest way. Why android doesn't center by default or make it a simple constructor param is beyond me.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("My Title");
builder.setMessage("My message");
builder.setPositiveButton("OK", listener);
AlertDialog dialog = builder.show();
// Must call show() prior to fetching views
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
if (titleView != null) {
titleView.setGravity(Gravity.CENTER);
}
- 4,848
- 2
- 31
- 32
we can use as like.
public static void showAlert(Activity activity, String message) {
TextView title = new TextView(activity);
title.setText("Your Title Here");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setCustomTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.show();
TextView messageText = (TextView)alert.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.RED);
}
- 31,008
- 6
- 34
- 52
- 7,083
- 4
- 31
- 50
you can try this code
public void Info(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Info Aplication");
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);
}
- 11,919
- 8
- 62
- 82
- 77
- 4
-
1Post a description above your code, so others can understand what you did – Juan Cruz Soler Jun 15 '16 at 23:26
-
Post a description above your code, so others can understand what you did. – de-russification Nov 07 '19 at 08:34
-
without using textview. worked for me.
styles.xml
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:gravity">center</item>
</style>
Alertdialog
final AlertDialog builder = new AlertDialog.Builder(activity,
R.style.CustomAlertDialog)
Just use that method and your dialog title and message will appear at center:
public static void openDialog(Context context, String message) {
TextView title = new TextView(context);
// You Can Customise your Title here
title.setText("Information Message");
title.setBackgroundColor(Color.BLACK);
title.setPadding(10, 15, 15, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(22);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCustomTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
// You Can Customise your Message here
TextView messageView = (TextView) alertDialog
.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
- 59
- 1
- 1