0

I have two button in my layout inside my TableLayout, but the text is not in center, I try to use android:gravity="center" and android android:layout_gravity="center", but seems it doesn't work.

How can i fix this? thanks in advance

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:id="@+id/bawah"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"

        >
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="10">
                <Button
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:text="Approve"
                    android:textColor="@color/putih"
                    android:textAlignment="center"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:layout_weight="4.9"
                    android:id="@+id/approve"
                    android:background="@drawable/backhijau"

                    />
                <TextView android:layout_height="wrap_content"
                    android:layout_width="0dip"
                    android:layout_weight="0.2"/>
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="4.9"
            android:textAlignment="center"
            android:text="Tolak"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/putih"
            android:id="@+id/tolak"
            android:background="@drawable/backmerah"
            />


            </TableRow>
        </TableLayout>
    </RelativeLayout>

enter image description here

Rucha Bhatt Joshi
  • 822
  • 1
  • 15
  • 38
Denny Kurniawan
  • 1,581
  • 2
  • 15
  • 28

2 Answers2

2

It's seems fine for me copy and past this code and let me know

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bawah"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="10dp"
    android:layout_marginRight="20dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="10">

            <Button
                android:id="@+id/approve"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="4.9"
                android:background="@color/colorAccent"
                android:gravity="center"
                android:text="Approve"
                android:textAlignment="center"
                android:textColor="@color/colorPrimary"

                />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.2" />

            <Button
                android:id="@+id/tolak"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="4.9"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:text="Tolak"
                android:textAlignment="center"
                android:textColor="@android:color/white" />

        </TableRow>


</RelativeLayout>

output

enter image description here

Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
2

No need of TableLayout, just use RelativeLayout and android:layout_below property and you will get result as you wanted. Also there is no need to implement weightSum property.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:id="@+id/bawah"
        android:layout_margin="10dp">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Approve"
                    android:textColor="@color/putih"
                    android:textAlignment="center"
                    android:id="@+id/approve"
                    android:background="@drawable/backhijau"

                    />
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Tolak"
                    android:layout_below="@+id/approve"
                    android:textColor="@color/putih"
                    android:id="@+id/tolak"
                    android:background="@drawable/backmerah"
                    />

    </RelativeLayout>
jack jay
  • 2,493
  • 1
  • 14
  • 27