33

i have to align text by middle in android alertdialog. but i cannot find way... anyone knows how to this?

coolhex
  • 331
  • 1
  • 3
  • 3

8 Answers8

84

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();

gotwo
  • 663
  • 8
  • 16
jeevamuthu
  • 2,054
  • 4
  • 19
  • 33
  • 11
    There might be AlertDialog dialog = builder.create(); instead of AlertDialog dialog = builder.show(); and dialog.show(); should be moved 2 lines above – goRGon May 19 '14 at 19:31
23

I know this thread is old but might help some people :D

TextView title = new TextView(this);
title.setText("Client details not saved!");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
// title.setTextColor(getResources().getColor(R.color.greenBG));
title.setTextSize(23);

TextView msg = new TextView(this);
msg.setText("You're going to lose all the information if you continue!");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);

DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            finish();
        }
    }

};

Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setView(msg);
builder.setCancelable(true);
builder.setPositiveButton("Yes", onClick);
builder.setNegativeButton("No", onClick);

AlertDialog dialog = builder.create();
dialog.show();
tar
  • 1,538
  • 1
  • 20
  • 33
Sorin
  • 767
  • 12
  • 17
  • Please can you write how your code is helpful and differs from other answers. It will help people read your answer and determine whether to try it or not (and might help them learn) – Patrick Mar 26 '13 at 15:49
  • How do you add the `title` TextView too in the `builder`? – Compaq LE2202x Sep 02 '13 at 03:34
4

Best way is to design Custom Dialog box.

This Custom alart Dialog

view_dialog_box.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="#A9E2F3">

<TextView
    android:id="@+id/txtDiaTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Connection Alart"
    android:textColor="@color/Black"
    android:textStyle="bold"
    android:gravity="center"
    android:padding="5dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="#2E9AFE"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    />

<TextView
    android:id="@+id/txtDiaMsg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:text="No Internet Connection"
    android:textColor="@color/Black" />

<Button
    android:id="@+id/btnOk"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="OK"
    android:textColor="@color/Black"
    android:textStyle="bold"
    android:padding="5dp" 
    android:layout_margin="5dp"
    android:background="@color/White"/>

Then it use in java file

final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.view_dialog_box);

    // set the custom dialog components - text and button
    TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle);
    TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg);

    Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();

        }
    }); 
    dialog.show();
SAndroidD
  • 1,745
  • 20
  • 33
4

You can use your custom layout for alert dialog layout. To align default alert dialog layout message center you can do

        AlertDialog alertDialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("hello world");
        alertDialog = builder.show();
        TextView messageText = (TextView) alertDialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);

Be careful, if you set messageText with findViewById before you call builder.show() you'll get a null pointer exception.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
4

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);

}
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
1

Have your TextView fill the parent and give it a center gravity.

<TextView ... android:layout_width="fill_parent" android:gravity="center" />
Jean
  • 10,545
  • 2
  • 31
  • 31
1

You would have to use one of the constructors provided for AlertDialog in Android, while creating one.

AlertDialog(Context context, int theme) Construct an AlertDialog that uses an explicit theme.

This link will help you. Since you want the text to be centered, you would want to give the gravity attribute, the value 'center'.

Community
  • 1
  • 1
Abhijit
  • 4,853
  • 3
  • 30
  • 33
0

Try this - it will do the trick.

AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this);

TextView resultMessage = new TextView(Main.this);
resultMessage.setTextSize(22);
resultMessage.setText("Upload completed!");
resultMessage.setGravity(Gravity.CENTER);
completeDialog.setView(resultMessage);

completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

    @SuppressLint("DefaultLocale")
    public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();               
    }

});

completeDialog.show();
tar
  • 1,538
  • 1
  • 20
  • 33
ksu
  • 882
  • 1
  • 11
  • 17