Gold
Gold

Reputation: 62424

Make a TextView and a Button the same size

I have a TextView and Button in my program and I cannot get the size of the Button and the TextView to be the same. How can I do this?

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="67dp"
        android:background="#999999" 
        android:gravity="center" 
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="130dp"
            android:layout_height="60dp"
            android:text="Button" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="130dp"
            android:layout_height="60dp"
            android:background="#ffffff" android:textColor="#000000"
            android:textSize="24dp" android:textStyle="bold"
            android:gravity="center"
            android:text="0.0" />
    </LinearLayout>

Upvotes: 0

Views: 1794

Answers (2)

Avinash
Avinash

Reputation: 174

When I want multiple controls to have the same size what i generally do is put them in a TableLayout.

In your case, i'd put a TableLayout inside the linear layout and put the button and textview inside a TableRow, set the weightsum for the TableRow to 2 and set the weights of the individual controls to 1.

This would make the controls take up the same amount of space on the screen. A sample xml is shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >



        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="2" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/hello_world">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />
        </TableRow>

    </TableLayout>

</LinearLayout>

Upvotes: 1

Andy Res
Andy Res

Reputation: 16043

Actually, they both have the same size, but the Button uses an image as a background and it seems it has some margins.

To test this, override the background of button with a color and see they are the same size:

android:background="#0F0"

So, the solution would be to provide a custom background for your button, or adapt the TextView to match the Buttons' width and height, minus the margins of Button, which personnaly I don't think is the best approach.

Upvotes: 2

Related Questions