Reputation: 1
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.
Upvotes: 0
Views: 3952
Reputation: 273
Adding <item name="android:gravity">center</item>
worked for me.
Upvotes: 0
Reputation: 1200
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);
}
});
}
Upvotes: 0
Reputation: 5045
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);
Upvotes: 0
Reputation: 848
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
Upvotes: 0
Reputation: 99
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.
Upvotes: 1