Bullionist
Bullionist

Reputation: 2200

Align button text to top/center horizontal android

enter image description here

Hi, I have a button which looks like img1.

I need the button's text to be top/horizonal center like img2.

I tried using this

<Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/equalBtn2"
                android:background="@drawable/blue_payment_button"
                android:textSize="40sp"
                android:textColor="@drawable/text_general"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button15"
                android:layout_toEndOf="@+id/button15"
                android:layout_weight="1"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginBottom="2dp"
                android:text="0"
                android:gravity="center_horizontal|top" />

But I get the result as img3. I dont want the top spacing between the text and the button's head. Please help. Thanks in advance.

Edit:

Button's parent XML

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="false"
            android:id="@+id/linearLayout5"
            android:layout_above="@+id/linearLayout3"
            android:layout_weight="1" >

            <Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="C"
                android:id="@+id/button13"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:background="@drawable/blue_payment_button"
                android:textSize="40sp"
                android:textColor="@drawable/text_general"
                android:layout_weight="1"
                android:layout_margin="2dp" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="0"
                android:id="@+id/button14"
                android:background="@drawable/yellow_general_button"
                android:textSize="40sp"
                android:textColor="@drawable/text_general"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button13"
                android:layout_toEndOf="@+id/button13"
                android:layout_weight="1"
                android:layout_margin="2dp" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="&lt;"
                android:id="@+id/button15"
                android:background="@drawable/blue_payment_button"
                android:textSize="40sp"
                android:textColor="@drawable/text_general"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button14"
                android:layout_toEndOf="@+id/button14"
                android:layout_weight="1"
                android:layout_margin="2dp" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/equalBtn2"
                android:background="@drawable/blue_payment_button"
                android:textSize="40sp"
                android:textColor="@drawable/text_general"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button15"
                android:layout_toEndOf="@+id/button15"
                android:layout_weight="1"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginBottom="2dp"
                android:text="0"
                android:gravity="center_horizontal|top" />
        </LinearLayout>

    </LinearLayout>

Upvotes: 2

Views: 4195

Answers (4)

Zain
Zain

Reputation: 40810

Create a custom style into styles.xml

<style name="textTop">
    <item name="android:gravity">top|center</item>
</style>

And set this style to the button

<Button
    style="@style/textTop"
    .... />

And never set the android:gravity to the button, it will dismiss the top mentioned gravity in style

Upvotes: 0

LEETAEJUN
LEETAEJUN

Reputation: 186

Try using this code (like badge style) : edit!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <TextView
    android:id="@+id/textOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/myButton"
    android:layout_centerHorizontal="true"
    android:text="0"/>
</RelativeLayout>

Upvotes: 1

chaitanya dalvi
chaitanya dalvi

Reputation: 1669

<Button
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/equalBtn2"
                android:background="@drawable/add_exp_pressed"
                android:textSize="40sp"
                android:textColor="@color/AliceBlue"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button15"
                android:layout_toEndOf="@+id/button15"
                android:layout_weight="1"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginBottom="2dp"
                android:paddingTop="0dp"
                android:text="Hello"
                android:layout_marginTop="-10dp"
                android:gravity="center_horizontal|top" />

Try this

Upvotes: 2

capt.swag
capt.swag

Reputation: 10651

The best way to do so is by setting minHeight="0dp" and setting android:paddingBottom="whatever dimension" you like.

Example:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="0dp"
    android:paddingBottom="16dp"
    android:text="O"
    android:background="@color/yellow" />

minHeight removes the default paddingTop and paddingBottom of button. So you could manually set paddingBottom so that text aligns to the top of the button.

Upvotes: 0

Related Questions