1

Possible Duplicate:
Button half width of screen

I want to create two buttons inside a linear layer (horizontal orientation), each one having the half width of the screen, and also a little margin. I have this xml layout:

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

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="5dp"
            android:text="@string/Title" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|fill_horizontal"
            android:orientation="horizontal"
            android:padding="2dp" >

            <Button
                android:id="@+id/btnLeft"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:text="@string/LeftButton" />

            <Button
                android:id="@+id/btnRight"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1" 
                android:text="@string/RightButton" />

        </LinearLayout>
    </LinearLayout>

Well, all this code works perfectly for what I want but I get a warning about my two buttons. It says that "Nested weights are bad for performance". I know what is it about but I don't know how to change my layout in order to get rid of the warning (and improve performance, I suppose) and keep both buttons having the half of the screen.

Any ideas? Thanks!

Community
  • 1
  • 1
ali
  • 10,927
  • 20
  • 89
  • 138

3 Answers3

6

Try this:

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

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="5dp"
        android:text="@string/Title" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnLeft"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/LeftButton" />

        <Button
            android:id="@+id/btnRight"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:text="@string/RightButton" />

    </LinearLayout>
</LinearLayout>
amp
  • 11,754
  • 18
  • 77
  • 133
  • I tried that too, but the warning is still there – ali Apr 22 '12 at 14:52
  • Use lint tool to find where is the erro becasue amp solution is the correct form to use layout_weight to avoid 2 builts of the views (buttons) – Aracem Apr 22 '12 at 15:16
2
<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|fill_horizontal"
            android:orientation="horizontal"
            android:padding="2dp" >

replace the above with below":::

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="2dp" android:weighSum = "2">
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • Not working. Maybe I should try Relative Layouts, even if I hate to re-edit the entire file – ali Apr 22 '12 at 14:55
1
<LinearLayout
    android:id="@+id/buttonHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/cmdSignup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Signup" />

    <Button
        android:id="@+id/cmdLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Login" />
</LinearLayout>
kishu27
  • 3,140
  • 2
  • 26
  • 41