Search code examples
androidandroid-layoutandroid-linearlayout

2 Views side by side with the same height


I'm using 2 inner Linearlayouts to divide my screen into 2 sections. It looks like this:

Screenshot

The first inner Layout has a TextView and a Button. The second one has a ImageView and also a Button. Now I want to get the same height for the TextView and ImageView without setting a fix value for layout_height. The left button should also be in line with the right button.

Here is my xml:

<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="horizontal"
          tools:context="de.dk.masterfi.ActMain">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:orientation="vertical">


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:background="@drawable/border"
        android:padding="10dp"
        android:text="@string/welcome"/>

    <Button android:id="@+id/button2" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="Favoriten"/>


</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="280dp" android:src="@drawable/training"/>

    <Button android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="Hauptmenü"/>

</LinearLayout>


Solution

  • try this, note the android:layout_weight="1" attribute for TextView and ImageView:

    <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="horizontal"
          tools:context="de.dk.masterfi.ActMain">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/border"
                android:padding="10dp"
                android:text="@string/welcome"/>
            <Button 
                android:id="@+id/button2" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Favoriten"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:layout_weight="1"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:src="@drawable/training"/>
            <Button
                android:layout_width="match_parent" 
                android:layout_height="wrap_content"
                android:text="Hauptmenü"/>
        </LinearLayout>
    </LinearLayout>