1

I have an AlertDialog title that will center vertically when the message content of the dialog does not need scrolled, as seen below:

enter image description here

Now when the message content needs to be scrolled the title is no longer vertically centered, as seen below:

enter image description here

I have tried setting the title and icon initially using the following:

builder.setTitle("Help");
builder.setIcon(R.drawable.help);

And the title does not center in the second situation with the above code. I have since tried to switch to an XML layout for the title and using builder.setCustomTitle(...); to set the title, with the following XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minHeight="72dp"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:paddingLeft="14dp">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/help"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Help"
    android:textSize="24sp"
    android:textColor="#ff33b5e5"/>

</LinearLayout>

I have tried the attributes minHeight, gravity, layout_gravity, and padding. I have been able to center the title in the second situation using the padding attribute but then the title in the first situation is no longer center. While padding could work, I would like to find a solution that will keep the title centered in each situation, independent of if the message needs scrolled. I feel there is something simple and/or straightforward that I have overlooked.

Also, the message content is simply set using builder.setMessage();.

EDIT 1: I should clarify that I do not want the title centered horizontally, only vertically with it left aligned plus a margin/padding. Ultimately I want each title bar to appear like the first image above.

EDIT 2: Here is the java code for the dialog, if it helps:

public class HelpDialog extends DialogFragment{
// Bundle variables/args
final static String ARG_HELP = "help";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.custom_help_title, null);

    Bundle args = getArguments();

    //initial way it was done
    //builder.setTitle("Help");
    //builder.setIcon(R.drawable.help);

    //now with the custom xml
    builder.setCustomTitle(view);
    builder.setMessage(args.getCharSequence(ARG_HELP));
    builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dismiss();
        }
    });

    return builder.create();
}
}
TronicZomB
  • 8,667
  • 7
  • 35
  • 50

3 Answers3

2
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    TextView title_txt = new TextView(this);
    title_txt.setGravity(Gravity.CENTER_HORIZONTAL);
    title_txt.setText("Hello");
    title_txt.setTextSize(16);
    builder.setCustomTitle(title_txt);
    builder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() 
    {
                @Override
         public void onClick(DialogInterface dialog, int which) 
         {
         }

    });
    builder.setNegativeButton("No",
            new DialogInterface.OnClickListener() 
   {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                dialog.dismiss();

            }
    });
   builder.show();
Mahesh Suthar
  • 237
  • 1
  • 11
  • This code work fine for title that appear in center.. Thanks – Mahesh Suthar Jun 30 '15 at 13:55
  • The TextView in this Code is created Dynamically So You dont need it in xml file... – Mahesh Suthar Jun 30 '15 at 13:57
  • This still pushes the title upwards, not centered, in the second situation described above. – TronicZomB Jun 30 '15 at 14:03
  • remove padding and then try – Mahesh Suthar Jun 30 '15 at 14:05
  • Is your Dialog message larger then the height of the screen, thus requiring you to scroll the message? That is when I am running into these problems, otherwise the title is perfectly fine. – TronicZomB Jun 30 '15 at 14:06
  • And i have edited my code first try it in simple activity – Mahesh Suthar Jun 30 '15 at 14:16
  • I did try a simplified version and it still did not work in the second situation described above. Have you had success in the above situation where the message needs scrolled? I have the title centered just fine in simple situations where the message does not need scrolled, it is the situation when it needs scrolled that the problem arises. – TronicZomB Jun 30 '15 at 14:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81983/discussion-between-mahesh-suthar-and-troniczomb). – Mahesh Suthar Jun 30 '15 at 14:21
0

Have you tried setting the gravity to just 'center'? Get rid of the padding etc.

Rohan
  • 593
  • 7
  • 22
  • This centers the title horizontally, however the vertical is still not centered in the title. – TronicZomB Jun 30 '15 at 13:22
  • Try setting the gravity value to center_horizontal|center_vertical – Rohan Jun 30 '15 at 13:26
  • That is what I have and am having issues with the view still not actually centering. – TronicZomB Jun 30 '15 at 13:28
  • Make the root a RelativeLayout. Inside this a LinearLayout whose width and height are wrap_content. The linear layout contains your icon and textview. Add the layout_centerInParent attrribute to the linearlayout and set it to true. – Rohan Jun 30 '15 at 13:36
  • That still did not work. Thanks for the effort though. – TronicZomB Jun 30 '15 at 13:40
0

So I ended up solving this with a more custom XML layout that contains the message TextView instead of using .setMessage().

The XML ended up being:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:padding="10dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:src="@drawable/help"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Help"
        android:textSize="24sp"
        android:textColor="#ff33b5e5"/>
</LinearLayout>

<View android:id="@+id/titleDivider"
    android:layout_width="match_parent"
    android:layout_height="2dip"
    android:background="#ff33b5e5" />

<TextView
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="16"
    android:minLines="3"
    android:scrollbars="vertical"
    android:fadeScrollbars="false"
    android:textSize="20sp"
    android:padding="7dp"/>

</LinearLayout>

And the Java code ended up as:

public class HelpDialog extends DialogFragment{
// Bundle variables/args
final static String ARG_HELP = "help";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.custom_help_title, null);

    Bundle args = getArguments();

    builder.setView(view);

    TextView message = (TextView) view.findViewById(R.id.message);
    message.setMovementMethod(ScrollingMovementMethod.getInstance());
    message.setText(args.getCharSequence(ARG_HELP));

    builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dismiss();
        }
    });

    return builder.create();
}
}

In order to get the scrolling for larger messages to function properly the solution from here was used.

This code gave me the following results:

enter image description here

enter image description here

Community
  • 1
  • 1
TronicZomB
  • 8,667
  • 7
  • 35
  • 50