Vlad
Vlad

Reputation: 525

Constraint Layout can't set width or height to 0

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

Upvotes: 2

Views: 2048

Answers (4)

Nishant Pardamwar
Nishant Pardamwar

Reputation: 1485

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"

Upvotes: 4

indra lesmana
indra lesmana

Reputation: 261

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

Upvotes: 0

Ashita Asati
Ashita Asati

Reputation: 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.

Upvotes: -1

indra lesmana
indra lesmana

Reputation: 261

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

Upvotes: -2

Related Questions