Ankit HTech
Ankit HTech

Reputation: 1893

Android fix the image size

In my screen I am having 3 buttons and in next row below those buttons I am having 3 names (name corresponding to those buttons) but my problem is that when ever name changes then size of buttons also changes but I do want to fix the size of buttons here is my code please help me

<TableLayout android:background="@drawable/toptab"
    android:layout_width="fill_parent" android:id="@+id/tableLayout"
    android:layout_height="wrap_content"
    android:stretchColumns="1" android:gravity="center_vertical" 
    android:layout_alignParentBottom="true">

    <TableRow>

        <ImageButton android:id="@+id/btnPrev" android:background="@drawable/imgPrev"
            android:layout_marginLeft="5dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             />


        <ImageButton android:id="@+id/btnRefresh" 
            android:layout_gravity="center" android:background="@drawable/refreshbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <ImageButton android:id="@+id/btnNext"
            android:background="@drawable/imgNext"
            android:layout_width="5dp"
            android:layout_height="20dp"
            android:layout_marginRight="5dp" />
    </TableRow>
    <TableRow >
        <TextView android:id="@+id/prev" android:text="Hk" android:layout_marginLeft="5dp" />
        <TextView android:id="@+id/refresh" android:text="Refresh" android:layout_gravity="center" />
        <TextView android:id="@+id/next" android:text="RS" android:layout_marginRight="5dp" />
    </TableRow>

</TableLayout>

Upvotes: 1

Views: 192

Answers (2)

slezadav
slezadav

Reputation: 6141

If you want to avoid fixing button size, then I would recommend using different layout than TableLayout, maybe RelativeLayout and TextViews with property alignBaseline or LinearLayout with weight properties could do the trick.

Upvotes: 2

Maneesh
Maneesh

Reputation: 6128

You should use linearlayout instead of table layout and use Weight property for aligning buttons and text view in equal dimensions, so here if you write more text in text view , button size will not increase. Also you can add more buttons in same manner. Below is the XML file and screen shot.

<?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" android:layout_weight="1" android:gravity="center_horizontal">

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextViewTextTextViewTextTextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" android:layout_weight="1" android:gravity="center_horizontal">
             <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" android:layout_weight="1" android:gravity="center_horizontal">
             <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

        </LinearLayout>

    </LinearLayout>

enter image description here

Upvotes: 1

Related Questions