Gorithis
Gorithis

Reputation: 21

Setting height of LinearLayout relative to other LinearLayout in the same container

I'm new at Android programming and I'm trying to set up a LinearLayout with four components inside.

<img src="https://i.imgur.com/jrkbH0F.png" />

Currently it looks like this:

<img src="https://i.imgur.com/yxSaYOS.png" />

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:weightSum="1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:id="@+id/linearLayoutLeft">

            <ImageView
                android:id="@+id/buildMonsterIcon"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:paddingBottom="5dp"
                android:paddingRight="15dp"
                android:paddingTop="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buildMonsterName"
                android:layout_marginBottom="10dp"
                android:paddingRight="10dp" />

    </LinearLayout>

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

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="?attr/actionBarTabBarStyle"
                android:id="@+id/buildToolbar">
                </android.support.v7.widget.Toolbar>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/buildList"
                android:paddingRight="5dp" />

        </LinearLayout>

</LinearLayout>

The problem here is, that my ListView currently has five items in it (more than the three items visible).

The only lead I had is setting the height of the ImageView to a fixed value (i.e. 200dp), then it looked like this:

Screenshot

How can I set the height of the left LinearLayout to be equal to the right one?

Thank you very much!

Upvotes: 1

Views: 44

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37414

You need to set weight property with value 1 for both of the left and right linear layout and set width as 0dp plus also change the weightsum to 2 or you can remove it as well

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:id="@+id/linearLayoutLeft">

            <ImageView
                android:id="@+id/buildMonsterIcon"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:paddingBottom="5dp"
                android:paddingRight="15dp"
                android:paddingTop="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buildMonsterName"
                android:layout_marginBottom="10dp"
                android:paddingRight="10dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/linearLayoutRight">

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="?attr/actionBarTabBarStyle"
                android:id="@+id/buildToolbar">
                </android.support.v7.widget.Toolbar>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/buildList"
                android:paddingRight="5dp" />

        </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions