Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center message in android dialog box

Tags:

java

android

I want the message text within my dialog box to be center aligned.

like image 649
Siva K Avatar asked Feb 10 '11 06:02

Siva K


People also ask

How do I center the title of dialog alert in Android?

You cannot center the title in the default alert dialog. You will need to create a custom dialog in order to center the title.

How many types of dialog are there in Android?

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)


2 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); 
like image 196
Chase Avatar answered Oct 09 '22 21:10

Chase


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.

like image 44
Zelimir Avatar answered Oct 09 '22 21:10

Zelimir