Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setLayoutParams on ConstraintLayout does not change size

Story

I have 2 textviews, one on top of another:

XXXXXXXXXXXX[TextView1]
XXXXXXXXXXXX[TextView2]
  • Both TextViews aligns to the right. (Hence the XXX above)
  • TextView1 has a background color.
  • TextView2 is always longer than TextView1
  • TextView1's left must be aligned with TextView2

I have the above 4 requirements. Now everything works well by using ConstraintLayout, where TextView1's left is constrained to TextView2's left, and TextView2's width is wrap_content.

But now it comes the 5th requirement:

  • TextView2 can disappear, leaving TextView1 behind.

setVisibility(GONE) does not work, because TextView2 must be wrap_content. it will become 0 in width and therefore TextView1 as well (since its left is constrained to TextView2's left)

Then I tried to set TextView2's height to be 0, using the below code:

    ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams)textView2.getLayoutParams();
    lp.width = widthInPixelOrConstant;
    textView2.setLayoutParams(lp);

Oops, nothing happens. TextView2 remains there, as if nothing happened. I Googled for half an hour, and finally decided to wrap TextView2 in a LinearLayout.

And then using the below code to set height of TextView2 to be 0:

    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams )view.getLayoutParams();
    lp.width = widthInPixelOrConstant;
    view.setLayoutParams(lp);

......It works!!!

The Question

So, apparently, we cannot use what we used before - setLayoutParams to try to alter the height or width of a ConstraintLayout's child at runtime.
But then, wrapping that TextView2 in a LinearLayout is really stupid.
Does anyone knows how to change a ConstraintLayout's child width or height at runtime?

like image 269
Sira Lam Avatar asked Oct 20 '17 09:10

Sira Lam


People also ask

How do I change ConstraintLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Is ConstraintLayout better than RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).


1 Answers

Try add constraints at runtime, like this:

//Define some variables
TextView textView1;
TextView textView2;
ConstraintLayout constraintLayout;

//Initialize them, and so some stuff
textView1 = (TextView) findViewById(R.id.text1);
textView2 = (TextView) findViewById(R.id.text2);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);

int x = 200; //set some width
int y = 200; //Set some height        

ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.WRAP_CONTENT,
            ConstraintLayout.LayoutParams.WRAP_CONTENT);

lp.width = x;
lp.height = y;

//moves TextView1's left to align with TextView2's left
lp.editorAbsoluteX = ((int) textView2.getX());     

textView1.setLayoutParams(lp);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);        

constraintSet.connect(R.id.text1,ConstraintSet.LEFT,R.id.text2,ConstraintSet.LEFT,0);
constraintSet.connect(R.id.text2,ConstraintSet.TOP,R.id.text1,ConstraintSet.BOTTOM,0);

constraintSet.applyTo(constraintLayout);

I hope that helps!

like image 137
Zerato Avatar answered Oct 17 '22 09:10

Zerato