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:

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);

Post a Comment for "How Do You Add A ConstraintLayout To A LinearLayout In A ScrollView That Fits The Whole Screen?"