Search code examples
androidandroid-linearlayoutandroid-constraintlayoutandroid-scrollview

How do you add a ConstraintLayout to a LinearLayout in a ScrollView that fits the whole screen?


I want to be able to scroll through a LinearLayout, where each element is a ConstraintLayout that fits the whole screen.

I've tried multiple values for layout_height in each Layout but nothing seems to be working. I could hardcode in a layout_height that fits my screen but that is not ideal.

Here's what I currently have:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</ScrollView>

Here's the design this produces:

enter image description here

although the design looks the way I want, I am unable to scroll down to see the succeeding ConstraintLayout.

any help is greatly appreciated. I've searched online and tried to fix this for hours with no results


Solution

  • Ok its very simple. You can't do that at xml, must do in code. The first create class below:

    public class DisplayMetricsUtils {
    
        private static DisplayMetricsUtils displayMetricsUtils;
        private static DisplayMetrics displayMetrics;
        private Context mContext;
    
        public DisplayMetricsUtils(Context mContext) {
            this.mContext = mContext;
            displayMetrics = mContext.getResources().getDisplayMetrics();
        }
    
        public static DisplayMetricsUtils newInstance(Context mContext) {
            if (displayMetricsUtils == null) {
                displayMetricsUtils = new DisplayMetricsUtils(mContext);
            }
            return displayMetricsUtils;
        }
    
    
        public int widthPixel() {
            return displayMetrics.widthPixels;
        }
    
        public int heightPixels() {
            return displayMetrics.heightPixels;
        }
    
        public float dpWidth() {
            return widthPixel() / displayMetrics.density;
        }
    
        public float dpHeight() {
            return heightPixels() / displayMetrics.density;
        }
    
        public void setHeightForView(View view, int ratio) {
            view.getLayoutParams().height = heightPixels() / ratio;
        }
    
        public void setWidthForView(View view, int ratio, int bonus) {
            view.getLayoutParams().width = widthPixel() / ratio + bonus;
        }
    
    
        public void setWidthForView(View view, int edge) {
            view.getLayoutParams().width = widthPixel() - edge;
        }
    
    
        public void setHeightForView(View view, int ratio, int bonus) {
            view.getLayoutParams().height = heightPixels() / ratio + bonus;
        }
    
    
        public int getHeightOfView(View view) {
            return view.getLayoutParams().height;
        }
    
    
    }
    

    And use this:

     DisplayMetricsUtils displayMetricsUtils = DisplayMetricsUtils.newInstance(UserActivity.this);
    
    displayMetricsUtils.setHeightForView("Your constaintlayout", 1);