Nested weights are bad for performance in my XML code

user2903095 picture user2903095 · Oct 28, 2013 · Viewed 11.5k times · Source

I want to display an organized grid. So I use LinearLayout and Layout Weight option, every thing works perfectly, but I don't understand how to avoid this warning in the Button A and the Button C:

Nested weights are bad for performance

And this is my XML code :

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

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

        <!-- First row. -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="A" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="B" />
        </LinearLayout>

        <!-- Second row. -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="D" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

How can I avoid this error please ?

Answer

Sherif elKhatib picture Sherif elKhatib · Oct 28, 2013

You can only ignore this warning if you want to keep this layout.

Add to the root of your layout: xmlns:tools="http://schemas.android.com/tools". Then in your buttons add tools:ignore="NestedWeights".

This can be also done in eclipse by putting the cursor on the yellow line and pressing ctrl + 1. You can choose ignore.


If you want to improve performance, you can use a TableLayout

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="5dip" >

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="A" />

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="B" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="5dip" >
            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="D" />
    </TableRow>
</TableLayout>