Search code examples
javaandroidxmlandroid-linearlayout

Android LinearLayout layout_weight behavior


I need to arrange 4 columns with views where the first one takes the whole height. The other 3 consist of 2 vertically placed views. I thought of using a parent horizontal LinearLayout consisting of the first view and 3 nested vertical LinearLayouts.

Now I need to make the last two columns half the size of the first two. So I used layout_weight and set it to 1 for the last two, and 2 for the first two. However this results in the last two columns taking up the entire space.

I guess this must have something to do with the rest of the layout settings, but due to my inexperience I can't quite put my finger on it. Hope one of you guys can help me out.

Here's the xml:

<LinearLayout
    android:layout_width="400dp"
    android:layout_height="100dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@id/table_playfield"
    app:layout_constraintTop_toBottomOf="@id/table_tile2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" >

    <com.example.se2_gruppenphase_ss21.game.TimerView
        android:id="@+id/timerView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2" />

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

        <Button
            android:id="@+id/remove"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/remove_tile"
            app:backgroundTint="#2196F3" />

        <Button
            android:id="@+id/ubongo_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:fontFamily="@font/architects_daughter"
            android:text="UBONGO !!"
            android:textSize="16sp"
            android:textStyle="bold"
            app:backgroundTint="#FF1010" />

    </LinearLayout>

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

        <Button
            android:id="@+id/mirror_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_vertical"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_left"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

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

        <Button
            android:id="@+id/mirror_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_horizontal"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_right"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_right"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

</LinearLayout>

Solution

  • Read Layout Weight

    Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0

    To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout).

        <LinearLayout
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@id/table_playfield"
        app:layout_constraintTop_toBottomOf="@id/table_tile2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" 
        android:weightSum="6"
       
        >
    
        <com.example.se2_gruppenphase_ss21.game.TimerView
            android:id="@+id/timerView2"
            android:layout_width=“0dp”
            android:layout_height="match_parent"
            android:layout_weight="2" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="2" >
    
            <Button
                android:id="@+id/remove"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/remove_tile"
                app:backgroundTint="#2196F3" />
    
            <Button
                android:id="@+id/ubongo_button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:fontFamily="@font/architects_daughter"
                android:text="UBONGO !!"
                android:textSize="16sp"
                android:textStyle="bold"
                app:backgroundTint="#FF1010" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1" >
    
            <Button
                android:id="@+id/mirror_vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/mirror_vertical"
                app:backgroundTint="#3F51B5" />
    
            <Button
                android:id="@+id/rotate_left"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/rotate_left"
                app:backgroundTint="#3F51B5" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
    
            <Button
                android:id="@+id/mirror_horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/mirror_horizontal"
                app:backgroundTint="#3F51B5" />
    
            <Button
                android:id="@+id/rotate_right"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/rotate_right"
                app:backgroundTint="#3F51B5" />
    
        </LinearLayout>
    
    </LinearLayout>
        
    

    FYI

    It will be better if you use android:weightSum.

    Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.