PeakGen
PeakGen

Reputation: 23025

Buttons are not in same size

I have created an application with buttons of "same size" and I added images to it which are also in the same size. Following is my code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeScreen" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        android:weightSum="4" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/fBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/fStr" />

            <Button
                android:id="@+id/rBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/rStr" />

            <Button
                android:id="@+id/sBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/sStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/cBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/cStr" />

            <Button
                android:id="@+id/aBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/aStr" />

            <Button
                android:id="@+id/lBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/lStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/oBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/oStr" />

            <Button
                android:id="@+id/tBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tStr" />

            <Button
                android:id="@+id/eBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/eStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/dBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/dStr" />

            <Button
                android:id="@+id/hBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/hStr" />

            <Button
                android:id="@+id/aBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/aStr" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

But the problem is, it does not generate what I expected. Following is how it get displayed in eclipse editor

enter image description here

Following is how it get displayed in emulator

enter image description here

As you can see, first column and third column are bigger than the second column. But what I expected is columns in same size.

Following is my strings.xml file in case it is helpful

<?xml version="1.0" encoding="utf-8"?>
<resources>


<string name="fStr">Fffffffff</string>
<string name="rStr">Rrrrrr</string>
<string name="sStr">Ssssss       </string>
<string name="cStr">Ccccccc</string>
<string name="aStr">Aaaaaaa</string>
<string name="lStr">Lllll</string>
<string name="oStr">Oooooooooooo</string>
<string name="tStr">Ttttt</string>
<string name="eStr">Eeeeee</string>
<string name="dStr">Dddddddddd</string>
<string name="cStr">Cccccccc</string>
<string name="hStr">Hhhh</string>
<string name="aStr">Aaaaaaaaaa aaaa</string>

</resources>

How can I solve this issue?

Upvotes: 2

Views: 215

Answers (6)

Prateek
Prateek

Reputation: 4013

Well I agree and recommend the use of layout_weight where you have to make a distribution or divide width among the view objects.

Also looking at your code I would suggest using lesser views as you are using TableLayout that is not actually required if you just use a LinearLayout with three buttons and use the above mentioned property properly.

I am also not sure why you actually need RelativeLayout in this case. If you go by hierarchy-viewer tool from android-sdk then you will come to know the lesser the number of views you are using a layout the low memory your screen will use and hence responsive.

The latter one is just a suggestion

Upvotes: 0

Jandeilson De Sousa
Jandeilson De Sousa

Reputation: 82

I think you can solve this issue adding

<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"

and into your LinearLayout(here) you can add your blocks with: android:layout_width="0.0dip" if you want to put some margin between the blocks... use: android:layout_marginLeft="sizehere.0dip" android:layout_marginRight="sizehere.0dip"

</LinearLayout>

Upvotes: 0

TMH
TMH

Reputation: 6246

Try setting the layout_width to 0dp, that's fixed this issue for me before.

Upvotes: 3

jyomin
jyomin

Reputation: 1967

Change the layout:width attribute of the table row with fill parent

 android:layout_width="fill_parent"

Upvotes: 0

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

try this way :

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

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

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

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

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

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

Upvotes: 0

bakriOnFire
bakriOnFire

Reputation: 2681

Change all the Button android:layout_width to fill_parent

android:layout_width="fill_parent"

Upvotes: 1

Related Questions