Button text is not centered vertically in the button

I am creating an Android app and I have a button with a custom background image behind it. The background image is just a rectangle, but now the button text does not seem to be centered vertically. Here is my xml for the button:

<Button 
android:id="@+id/save_to_list"
android:layout_below="@+id/info_pic"
android:text="SAVE TO LIST"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="260dp"
android:layout_height="35dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"></Button>

and I set the background here:

ImageView button_bg = (ImageView)findViewById(R.id.button_background);
savetolist_button.setBackgroundDrawable(button_bg.getDrawable());

Is there a place where you can change the position of the text inside the button?

+5
source share
5 answers

android:layout_gravitydetermines the alignment of the layout inside it parent. To align the text inside the button you need android:gravity, for example

<Button 
    ...
    android:gravity="center"
    ...>
</Button>
+12
source

, . layout_height .

+4

The text probably has a part of the supplement embedded in the font as part of each letter, and therefore your text is pushed in one direction. Set layout_height to "wrap_content" and your text should now be correctly centered.

+1
source

I found that it is better to use 9 patch images for a rectangular background rather than for xml drawing.

0
source

You can set gravity in the center:

android:gravity="center"
0
source

All Articles