androidandroid-linearlayoutandroid-relativelayoutandroid-layout-weight

How to make a Linear and a Relative Layout share horizontal screen space as percentages?


How to make a Linear and a Relative Layout share horizontal space as percentages?

The LinearLayout on the left needs to have a percentage width of the screen, and the RelativeLayout on the right can just take up what remains. I had this as two LinearLayout's and it worked fine because I could use the layout_weight attribute, defining width as a percentage, sorta like so:

android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight=".20"

But now I need the layout on the right to be a RelativeLayout, and can't figure out how to preserve the desired percentages since RelativeLayout does not have a layout_weight attribute. What I need looks something like:

enter image description here


Solution

  • Note: It's better to use ConstraintLayout now without worring about nested views.

    In RelativeLayout documentation:

    Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

    Here's the ConstraintLayout version:

    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/teal_700"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintWidth_percent="0.2" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/teal_200"
            app:layout_constraintStart_toEndOf="@+id/view1"
            app:layout_constraintWidth_percent="0.8" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    To answer for the question conditions if no way to use ConstraintLayout:

    Using android:layout_weight in a View that is wrapped in a RelativeLayout is prohibited as RelativeLayout doesn't provide that attribute for the underlying views (notice attributes start with layout_ are back to the parent layout which is RelativeLayout).

    The same way, using android:layout_weight in aRelativeLayout that doesn't mean that it's back to the RelativeLayout itself, but to its parent layout which is already a LinearLayout

    So, this will work normally either it's LinearLayout or a RelativeLayout:

    <?xml version="1.0" encoding="utf-8"?>
     
    <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:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="1">
     
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.2"
            android:background="@color/teal_700" />
     
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.8"
            android:background="@color/teal_200">
     
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is some text in Relative Layout" />
     
        </RelativeLayout>
     
     
    </LinearLayout>