1

I have two linear layouts directly inside a relative layout.
I want the first LinearLayout to occupy 75% of the height and the next 25%. How do I achieve this?

For example

<RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"       
        android:weightSum="100"
        android:id="@+id/contentMainLayout">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="75"
            android:id="@+id/linearLayout1">     
            <ScrollView
                android:id="@+id/mainScrollView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_margin="10dp"    
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <LinearLayout
                    android:id="@+id/mainScrollLL"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">                    
                </LinearLayout>
            </ScrollView>       
        </LinearLayout>
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="25"
            android:layout_marginBottom="20dp" 
            android:layout_below="@+id/mainScrollParentLL"
            android:orientation="horizontal">

            <Button
                android:id="@+id/startTrade"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Stop Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>

</RelativeLayout>

I want the linearLayout1 to use 75% of the height avaialble and the linearLayout2 to use 25%.
layout_weight is not defined for LinearLayout, so obviously it doesnt work.

Is there any way I can achieve this?

This seems to be a pretty common scenario, so I am almost sure this question has been asked before.
But I don't seem to find it. Please point me to the original question in case this is a duplicate one, I'll close this.

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54

3 Answers3

1

weightSum is not a property of Relative layout. You have to use linear layout as parent layout and then you can achieve your goal.

Reference - I had similar problem solved using this solution

1

You can try below code - you need to Change RelativeLayout to LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/contentMainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout

        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="75">

        <ScrollView
            android:id="@+id/mainScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:id="@+id/mainScrollLL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"></LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_weight="25"
        android:orientation="horizontal">

        <Button
            android:id="@+id/startTrade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Trade"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Stop Trade"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
</LinearLayout>
Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
1

Please Try this...

<LinearLayout
    android:id="@+id/contentMainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="75">

        <ScrollView
            android:id="@+id/mainScrollView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:id="@+id/mainScrollLL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"/>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="25"
        android:orientation="horizontal">

        <Button
            android:id="@+id/startTrade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Trade"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Stop Trade"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

</LinearLayout>