Search code examples
androidxmlandroid-studioandroid-linearlayout

Linear Layouts not obeying weighting


I have two vertical linear layouts which I want to use to occupy roughly a third and two thirds of the screen respectively. I am using layout_weight to achieve this but they are both remaining at 0dp thickness rather than occupying the available width of the parent.

To my understanding these layouts, when set to 0dp thickness, should occupy layout weighting / total weightingworth of the weight. This is, however, not happening.

Here is my xml, the first layout contains an ImageButton and the second is empty:

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:layout_gravity="left"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="false"
            android:cropToPadding="false"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/silhouette" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:layout_gravity="right"
        android:orientation="vertical">


    </LinearLayout>

Solution

  • Try this layout(Vertical Orientation)

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="left"
        android:layout_weight="0.3"
        android:orientation="vertical">
    
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="false"
            android:cropToPadding="false"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/silhouette" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="right"
        android:layout_weight="0.6"
        android:orientation="vertical">
    
    
    </LinearLayout>
    

    For horizontal orientation

     <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="0.3"
        android:orientation="vertical">
    
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="false"
            android:cropToPadding="false"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/silhouette" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="0.6"
        android:orientation="vertical">
    
    
    </LinearLayout>