0

I have created a custom title for my AlertDialog box. But I have not been able to centre the message. It always comes left aligned.

    TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
    String ScoreTitle = getResources().getString(R.string.AlertTitle);

    AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
    Context mContext = alrtbuilder.getContext();
    LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
    View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
    TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
    mTextView.setText(ScoreTitle);

The AlertDialog box has a positive and a negative button

    alrtbuilder.setCustomTitle(mView);
    alrtbuilder.setMessage(vehicle_no.getText().toString().toUpperCase()+" "+"?")
               .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    insert_timecard();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show;
John
  • 740
  • 5
  • 13
  • 36

3 Answers3

1

Add android:gravity="center" to TextView with id R.id.title_text in your layout R.layout.simple_dialog.

 <TextView
            android:id="@+id/title_text"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="wrap_content" />

To center default message TextView you have to get the TextView from dialog after show().

 alertDialog.show();
            TextView messageView = (TextView)alertDialog.findViewById(android.R.id.message);
            messageView.setGravity(Gravity.CENTER);

I do not know if its a good approach but you can also set the custom view to whole dialog then things can be handled easily.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • I don't want to centre the title_text. I want to centre the setMessage part setMessage(vehicle_no.getText()) – John May 02 '18 at 03:11
1

Add:

mTextView.setGravity(Gravity.CENTER_HORIZONTAL);

TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);

AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setText(ScoreTitle);

This should resolve:

Community
  • 1
  • 1
1

how about adding this?

TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
Sakura Fukuyoshi
  • 1,461
  • 1
  • 10
  • 14