Question

What i want to achive is to have a TextView, that takes about 70% of the screen, and the rest of the 30 % is divided EQUALY between 2 buttons set side by side (horizontally). The only way I achieved this was using the code below...however the xml editor complains about the nested wights...altough it works I am understanding that this is not good practice...How should i do it instead ?

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/news"
      android:textSize="14sp"
      android:textStyle="bold"
      android:textColor="#0033CC"
      android:layout_height="0px"
      android:layout_width="fill_parent"
      android:layout_weight="70"
      android:gravity="top"
/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:orientation="horizontal"
    android:layout_weight="30">
    <Button
        android:id="@+id/new_order_button"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/new_order" 
    />

    <Button
        android:id="@+id/previous_orders_button"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/previous_orders" 
    />
</LinearLayout >

EDIT : UPDATED CODE...warning still present

Thanks :)

Was it helpful?

Solution

You are receiving the warning from Lint because computing nested weights is expensive, and the most nested portion must be computed before the next layer can be. This delays drawing the layout to the screen, and is bad for performance.

You can get around this by changing your layout design. Rather than have the Buttons on the bottom arbitrarily take up 30% of the screen, you can have them take up just the layout_height they need with wrap_content, or set them to a static height using #dp. The TextView at the top will then take up the rest of the top portion of the screen. This is easier for Android to draw, as there aren't any nested weights to compute. Your Lint warning should go away:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/news"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:textColor="#0033CC"
        android:textSize="14sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/new_order_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="@string/new_order" />

        <Button
            android:id="@+id/previous_orders_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="@string/previous_orders" />
    </LinearLayout>

OTHER TIPS

Actually I think the editor is complaining because having nested weights it's not efficient. Layout weights require a widget to be measured twice and if you have nested weights this is increased exponencially.

as a best practice, you should not change size (height) of controls (in this case buttons) neither absolute nor relative - android will make it for you. In your case I simply would create a FlowLayout as the root container and then put the LinearLayout with two buttons (yielding 50% of space each) at the bottom. The TextView you can then layout to the top of the LinearLayout with the buttons occupying remaining space. However, you still can nest weighted layouts but its suboptimal to the performance of rendering.

One other possibility is to use a LinearLayout with 2 TableRows inside and then distribute the weights to the TableRows like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70" >

        <TextView
            android:id="@+id/news"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="top"
            android:textColor="#0033CC"
            android:textSize="14sp"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" >

        <Button
            android:id="@+id/new_order_button"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />

        <Button
            android:id="@+id/previous_orders_button"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2" />
    </TableRow>

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top