Search code examples
androidlayoutandroid-linearlayout

LinearLayouts: One fixed and one with variable height


I would like to add a top bar with two buttons in a vertical LinearLayout with the height of exactly 100 dp.

So I would like a 100 dp high view, and a variable high view below it.

I am trying this code, but it doesn't work. I can see that it doesn't accept 100 dp as a valid value, but I don't know how to achieve what I would like to do with LinearLayout.

<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100 dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </View>
</LinearLayout>

Solution

  • Try after remove space in 100dp

    <?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:orientation="vertical">
    
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:orientation="horizontal">
    
            <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Button 1" />
    
            <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Button 2" />
        </LinearLayout>
        <View
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        </View>
    </LinearLayout>