Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button text vertical alignment is off in Android

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:

button.jpg

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>
like image 625
jvergeldedios Avatar asked May 18 '11 07:05

jvergeldedios


2 Answers

That worked for me:

android:gravity="fill_vertical"

Hope it helps!

like image 154
QuarK Avatar answered Sep 28 '22 14:09

QuarK


Try this:

android:includeFontPadding="false"

It helped me with a similar problem.

like image 38
markussvensson Avatar answered Sep 28 '22 12:09

markussvensson