Search code examples
androidandroid-layoutbuttonline-breaks

Carriage return in Button messes with the margin


If I add a carriage return in a button an extra bit of "margin" (for a lack of better explanation) appears at the top:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff992a2a">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Extend" />
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Accept
0:23"/>
    </LinearLayout>
</RelativeLayout>

enter image description here

If I don't add the carriage return, it aligns perfectly: enter image description here

how can I stop the extra 'margin' appearing ? I tried setting the android:lines="2" and and/or android:singleline="false" but nothing seems to help.


Solution

  • You need to set android:gravity="center" in your LinearLayout

    SAMPLE CODE

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff992a2a">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:gravity="center"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="250dp"
                android:layout_height="100dp"
                android:text="Extend" />
    
            <Button
                android:layout_width="250dp"
                android:layout_height="100dp"
                android:text="Accept
    0:23" />
        </LinearLayout>
    </RelativeLayout>