0

Why doesn't Linear Layout maxHeight work? maxHeight does not work on RecyclerView

Height of section2 becomes 0 as I followed the instructions from above post.

My View Requirement is to have a:

  • TopBar followed by
  • 3 sections
  • Section1 wraps height followed by
  • Section2 maxHeight to 200dp or take/fill available height (followed by Section3)
  • Section3 height 45dp and aligns to bottom and
  • All 3 sections must be visible.
  • This entire view should center_parent of rootView of the activity.

  • Section1 is correct
  • Section3 is correct
  • But section2 height becomes 0 and is not visible if I don't set the min constraint.
  • If I set minHeight constraint for section2, section3 becomes invisible.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_home_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple"
    tools:context="mose.moseinc.com.Tymeit.Activities.Home.Activity">
    <RelativeLayout
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:background="@color/aqua_blue"
        android:layout_height="wrap_content">
        <mose.moseinc.com.Tymeit.Views.Home.TopBar
            android:id="@+id/top_bar_home_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </mose.moseinc.com.Tymeit.Views.Home.TopBar>
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/mainView"
            android:layout_below="@+id/top_bar"
            android:orientation="vertical"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <CustomView1
                android:id="@+id/section1"
                android:layout_width="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:background="@color/green_theme"
                app:tileHeight="40dp"
                android:layout_height="wrap_content" />
            <CustomView2
                android:id="@+id/section2"
                app:layout_constraintTop_toBottomOf="@+id/section1"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="50dp"
                android:layout_width="match_parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="200dp"
                android:layout_height="0dp">
            </CustomView2>
            <CustomView3
                android:id="@+id/bottomBar"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_width="match_parent"
                android:layout_height="45dp">
            </CustomView3>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </RelativeLayout>
</RelativeLayout>
GJain
  • 5,025
  • 6
  • 48
  • 82
  • https://stackoverflow.com/questions/4054567/android-why-is-there-no-maxheight-for-a-view/13811461 – GJain Jan 29 '21 at 00:05

1 Answers1

0

android:layout_height="0dp" and app:layout_constraintHeight_max="200dp" dont work together. Layout_height will always take prio and set the height of CustomView2 to 0.

What you need is to set android:layout_height="parent". Then it will try to take all available space it can get on the screen, but not exceed 200dp.

You set CustomView3 to visibility="gone" this does not match your requirement of visibility.

FunnyO
  • 383
  • 2
  • 20
  • I made it visible in code. I will make the edit my post above. But the posts I linked suggests that they work together. I can not set android:layout_height to "parent". – GJain Jan 28 '21 at 21:06
  • Thing is, that they may work in a ConstraintLayout. You are using a RelativeLayout. – FunnyO Jan 28 '21 at 21:09
  • My 3 sections are inside constraintLayout. I also tried setting layout_height of section2 to fill, match. It does not work. – GJain Jan 28 '21 at 21:12