1

I am new to android. Now i get warning for Nested weight bad performance.I have only one image and three buttons in my first screen. PLs give me some idea.


<LinearLayout 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:background="@color/backgroundColor"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:weightSum="10"
    >


    <ImageView
        android:layout_weight="4"
        android:id="@+id/imageLogo"
        android:layout_width="wrap_content" 
        android:layout_height="0dip"
        android:contentDescription="@string/Img"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/home_screen_logo" />

    <LinearLayout
       android:layout_weight="6"
       android:layout_width="fill_parent"
    android:layout_height="0dip" 
    android:orientation="vertical" 
    android:gravity="center_horizontal"
        >

        <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"
            android:id="@+id/temp"></LinearLayout>
    <ImageButton
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/login"
        android:contentDescription="@string/Img"  
        android:layout_weight="1"
        />
    <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"></LinearLayout>
    <ImageButton
        android:id="@+id/btnFbLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
         android:background="@drawable/btnfb"
         android:contentDescription="@string/Img"
        />
    <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"></LinearLayout>
    <ImageButton
        android:id="@+id/btnRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/btnregister"
        android:contentDescription="@string/Img"/>
</LinearLayout>
</LinearLayout>


Thanks in Advance

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

3 Answers3

6

Nested weights are bad for performance because:

Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.

It's always better to use RelativeLayouts and adjust your view according to the places of other views without using specific dpi values.

courtesy: Why are nested weights bad for performance? Alternatives?

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
0

Nested weights are bad for performance because:

Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.

It's always better to use RelativeLayouts and adjust your view according to the places of other views without using specific dpi values.

Refer: http://developer.android.com/resources/articles/layout-tricks-efficiency.html

You can use a RelativeLayout in most cases to avoid such expensive measurements. In a RelativeLayout, views are aligned with their parent, with the RelativeLayout itself, or with other views.

To clearly understand how the views are positioned with respect to each other, a wireframe of the layout can be captured by using HierarchyViewer perspective of Android SDK.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Try removing the android:weightSum="10" from the linear layout, also remove android:layout_weight="1"

Now you see that the warning has disappeared!

Make sure to keep the weights in the buttons and other stuff in linear layouts!

I hope that this may solve your problem :)

Nick Dickinson-Wilde
  • 1,015
  • 2
  • 15
  • 21
marios
  • 1