I’m having trouble with button text alignment in my Android app. I have a button defined in XML like this:
<Button
android:id="@+id/reportanissuebutton"
android:layout_width="272sp"
android:layout_height="32sp"
android:text="@string/button_report_issue"
android:textColor="#fff"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal"
android:background="@xml/report_issue_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40sp"/>
The result is this:
I was expecting the text to be centered both vertically and horizontally as it does with all of the other buttons in my application, but for some reason with this one it is offset slightly down. I can’t seem to figure out why its doing this, so any help would be appreciated.
Edit:
Here is the XML for the background of the button:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="6dp" />
<stroke android:color="#c2c2c2" android:width="2dp" />
<gradient
android:angle="90"
android:startColor="#000"
android:endColor="#919191"/>
</shape>
Try this: dip will size on different devices much better than sp and declaring no gravity is by default completely centered
Additionally: This exact thing happened to me when the font was too tall for the button try decreasing the size of the font slightly and/or increasing the height of the button…. this solved my issue.