Reputation: 327
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();
}
Upvotes: 12
Views: 20312
Reputation: 190
For Material Alert Dialog would be like this:
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
.setTitle("Titulo")
.setIcon(R.drawable.people)
.setMessage("un mensaje cualquiera");
AlertDialog dialog = builder.show();
((TextView) dialog.findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
Upvotes: 0
Reputation: 504
You can try this simple method:
textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
textView.setGravity(Gravity.CENTER);
alert.setView(textView);
Upvotes: 0
Reputation: 77
you can try this code
public void Info(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Info Aplikasi");
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);
}
Upvotes: 1
Reputation: 1928
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();
Upvotes: 16
Reputation: 3562
Instead of setting the message, use AlertDialog.Builder.setView(View)
with a View
with centred text
Upvotes: 6