Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

publicclassDisplayMetricsUtils {

    privatestatic DisplayMetricsUtils displayMetricsUtils;
    privatestatic DisplayMetrics displayMetrics;
    private Context mContext;

    publicDisplayMetricsUtils(Context mContext) {
        this.mContext = mContext;
        displayMetrics = mContext.getResources().getDisplayMetrics();
    }

    publicstatic DisplayMetricsUtils newInstance(Context mContext) {
        if (displayMetricsUtils == null) {
            displayMetricsUtils = new DisplayMetricsUtils(mContext);
        }
        return displayMetricsUtils;
    }


    publicintwidthPixel() {
        return displayMetrics.widthPixels;
    }

    publicintheightPixels() {
        return displayMetrics.heightPixels;
    }

    publicfloatdpWidth() {
        return widthPixel() / displayMetrics.density;
    }

    publicfloatdpHeight() {
        return heightPixels() / displayMetrics.density;
    }

    publicvoidsetHeightForView(View view, int ratio) {
        view.getLayoutParams().height = heightPixels() / ratio;
    }

    publicvoidsetWidthForView(View view, int ratio, int bonus) {
        view.getLayoutParams().width = widthPixel() / ratio + bonus;
    }


    publicvoidsetWidthForView(View view, int edge) {
        view.getLayoutParams().width = widthPixel() - edge;
    }


    publicvoidsetHeightForView(View view, int ratio, int bonus) {
        view.getLayoutParams().height = heightPixels() / ratio + bonus;
    }


    publicintgetHeightOfView(View view) {
        return view.getLayoutParams().height;
    }


}

And use this:

DisplayMetricsUtilsdisplayMetricsUtils= DisplayMetricsUtils.newInstance(UserActivity.this);

displayMetricsUtils.setHeightForView("Your constaintlayout", 1);

Post a Comment for "How Do You Add A Constraintlayout To A Linearlayout In A Scrollview That Fits The Whole Screen?"