Search code examples
javaandroidandroid-layoutconstraintsandroid-constraintlayout

Set max height on constraint layout


I have a listview inside constraint layout. When it has more lists, want to restrict the parent layout height. How can I set max layout height programmatically for bottom_layout? Display a maximum of 80% of device height.

This 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 on calling onCreate but didn't work:

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 you do this on layout by wrapping the ConstraintLayout in another one that has a height that matches the screen height.

    Then add the below constraints to the inner ConstraintLayout to get a max height of 80% of the total screen height. This requires to have a height that match constraints

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

    So, the outer layouts would 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>