0

How to center vertical the "Login" title?

This is the style:

<style name="ABGAlertDialog" parent="@android:style/Theme.DeviceDefault.Dialog">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="android:background">@color/corporativo_red</item>
    <item name="android:windowTitleStyle">@style/MyAlertDialogTitle</item>
</style>

<style name="MyAlertDialogTitle">
    <item name="android:background">@color/corporativo_red</item>
    <item name="android:colorBackground">@color/corporativo_red</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textStyle">bold</item>
    <item name="android:maxLines">1</item>
    <item name="android:textSize">20sp</item>
    <item name="android:bottomOffset">5sp</item>
</style>

I've tried gravity, textAlignment and some others items with no result.

Blo
  • 11,903
  • 5
  • 45
  • 99
user2655890
  • 1
  • 1
  • 4

4 Answers4

1

You can't centre a normal alert dialog's title. You will need to create a custom dialog preferably with a dialog fragment or dialog activity depending on the functionality you need.

KXIII
  • 99
  • 7
0

Set your text style in the android:textAppearance

<style name="MyAlertDialogTitle">
    <item name="android:background">@color/corporativo_red</item>
    <item name="android:colorBackground">@color/corporativo_red</item>
    <item name="android:bottomOffset">5sp</item>

    <item name="android:textAppearance">@style/YourTextStyle</item>
</style>

Change both text and background color in title bar (Android)?
Textview: using the dialog title style: @android:style/TextAppearance.DialogWindowTitle

Community
  • 1
  • 1
Milos Fec
  • 828
  • 6
  • 11
0

If Alert dialog is normal without any of your custom layout or theme than you ca get object by passing id with android namespace

AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity());
                alertDialogBuild.setMessage("message text align center");
                alertDialogBuild.setTitle("Title text Align Center");
                alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = alertDialogBuild.show();
                TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android"));
                title.setGravity(Gravity.CENTER);


                TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android"));
                message.setGravity(Gravity.CENTER);
user1140237
  • 5,015
  • 1
  • 28
  • 56
0
static void centerTitleHorizontally(final TextView title)
{
    ViewTreeObserver vto = title.getViewTreeObserver();
    vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            try {
                title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } catch (NoSuchMethodError x) {
                title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            Rect bounds = new Rect();
            title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds);
            title.setPadding((title.getWidth() - bounds.width()) / 2, 0, 0, 0);
        }
    });
}
doctorram
  • 1,112
  • 14
  • 13