Java – Set the maximum height on the constraint layout

Set the maximum height on the constraint layout… here is a solution to the problem.

Set the maximum height on the constraint layout

I have a ListView in the constraint layout. When it has more lists, limit the parent layout height. How do I programmatically set the maximum layout height for bottom_layout? Display up to 80% of the device height.

Here is the layout:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

<Button
            android:id="@+id/try_again"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/primary_button"
            android:minWidth="180dp"
            android:text="@string/error_try_again"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

<ListView
            android:id="@+id/instruction_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/try_again"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/instruction_subtitle" />

<TextView
            android:id="@+id/instruction_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

<TextView
            android:id="@+id/instruction_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/instruction_title"
            app:layout_constraintBottom_toTopOf="@+id/instruction_subtitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

Tried this solution when calling onCreate, but without success:

private void BottomCardHeight(){
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        float density  = metrics.density;
        float device_height  = metrics.heightPixels / density;

bottom_layout = findViewById(R.id.bottom_layout);
        ConstraintSet set = new ConstraintSet();
        set.clone(bottom_layout);
        set.constrainMaxHeight(R.id.bottom_layout, (int) (device_height * 0.8));
         set.constrainHeight(R.id.bottom_layout, (int) (device_height * 0.8)); both not working
        set.applyTo(bottom_layout);

ConstraintLayout mConstrainLayout  = (ConstraintLayout) findViewById(R.id.bottom_layout);
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mConstrainLayout.getLayoutParams();
        lp.matchConstraintMaxHeight = (int) (device_height * 0.8);
        mConstrainLayout.setLayoutParams(lp);
    }

Solution

You can do this by wrapping the ConstraintLayout in another layout whose height matches the height of the screen.

Then add the following constraint to the internal ConstraintLayout to get a maximum height of 80% of the total screen height. This requires a height that matches the constraint

android:layout_height="0dp"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"

Therefore, the external layout will be:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="wrap"
        app:layout_constraintHeight_percent="0.8"
        app:layout_constraintStart_toStartOf="parent">

<!-- other views -->

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Related Problems and Solutions