Story
I have 2 textviews, one on top of another:
XXXXXXXXXXXX[TextView1]
XXXXXXXXXXXX[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:
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?
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.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With