2

How can i set real width or height to 0 in ConstraintLayout programatically via its LayoutParams since the fact that ConstraintLayot.LayoutParams.MATCH_CONSTRAINT = 0 ?

I want to use in my code something like this:

view.getLayoutParams().width = 0;
view.requestLayout();
Vlad
  • 497
  • 7
  • 12

4 Answers4

4

I found a workaround for this. So according to this

<attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>

-1 is used for match_parent

and

-2 is used for wrap_content

so we can use -3 for setting 0 or less than 0 value for width or height.

e.g.

android:layout_width="-3dp"
Nishant Pardamwar
  • 1,450
  • 12
  • 16
0

this is example code to try, it works for me

val params = ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_CONSTRAINT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
                )
                val space16 = binding.root.resources.getDimensionPixelOffset(R.dimen.space_16)
                params.setMargins(space16, 0, 0, 0)

                /*app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/imgIcon1"
                app:layout_constraintTop_toTopOf="parent"*/

                params.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
                params.startToEnd = binding.imgIcon1.id
                params.topToTop = ConstraintLayout.LayoutParams.PARENT_ID


                binding.containerBottom.layoutParams = params
indra lesmana
  • 246
  • 2
  • 7
-1

If by "height" you mean the absolute top-to-bottom dimension of the ImageView, you can do the following to double to height of an ImageView. You can set height to the value that you want.

ImageView iv = (ImageView) findViewById(R.id.imageView); 
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) 
iv.getLayoutParams(); lp.height = 0 ; iv.setLayoutParams(lp);

See ConstraintLayout.LayoutParams.

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
-2

if you want change width to MATCH_CONSTRAINT you need set layout with horizontal constraint

layout_constraintStart_toStartOf or layout_constraintStart_toEndOf

and

layout_constraintEnd_toEndOf or layout_constraintEnd_toStartOf

indra lesmana
  • 246
  • 2
  • 7