I have an AlertDialog
title that will center vertically when the message content of the dialog does not need scrolled, as seen below:
Now when the message content needs to be scrolled the title is no longer vertically centered, as seen below:
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();
}
}