I'm having some difficulty with a runtime-generated UI that isn't following constraints. Per other answers and documentation for Android, I have tried ConstraintSet
as well as some other methods.
I am extending ConstraintLayout
for a custom class that has an integral part in the app's functionality. I pull data from a web server that then creates these objects and adds them to a LinearLayout
. The LinearLayout
, ConstraintLayout
, and TextView
s all show their proper sizes; however, the TextView
s overlap one another and refuse to follow any constraints.
I can create a copy of what I want to do manually through the layout editor, and it works fine.
My current setup (this
extends ConstraintLayout, text_name
and text_points
are TextView
s):
this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
this.addView(text_name);
this.addView(text_points);
ConstraintSet constraints = new ConstraintSet();
constraints.clone(this);
constraints.constrainHeight(text_points.getId(), WRAP_CONTENT);
constraints.constrainWidth(text_points.getId(), WRAP_CONTENT);
constraints.constrainHeight(text_name.getId(), WRAP_CONTENT);
constraints.constrainWidth(text_name.getId(), WRAP_CONTENT);
constraints.connect(text_name.getId(), LEFT, getId(), LEFT);
constraints.connect(text_points.getId(), RIGHT, getId(), RIGHT);
constraints.applyTo(this);
constrainWidth
and constrainHeight
work; when I get rid of them or change the values, it has an effect. Any of the connect
business, however, does nothing. The TextView
s have the right sizes for their text, but fail to obey any constraints. Am I missing some line that makes .connect
s work?
To clarify: I have ScrollView
-> LinearLayout
-> ConstraintLayout
-> TextView
s. When created in-editor, constraints work fine. When created in the instantiation of the ConstraintLayout
at runtime using a ConstraintSet
, connected constraints have no effect, but other properties do.
EDIT: Just to confirm, here's an output of the # of rows added to the LinearLayout (LOCS), size of the LinearLayout (LDIM), size of the ConstraintLayout (LOCDIM), and sizes of the TextViews (TVDIM, TV2DIM):
09-03 14:19:18.664 D/LOCS: 2
09-03 14:19:18.665 D/LDIM: 1080 w
09-03 14:19:18.665 D/LDIM: 58 h
09-03 14:19:18.665 D/LOCDIM: 1080 w
09-03 14:19:18.666 D/LOCDIM: 29 h
09-03 14:19:18.667 D/TVDIM: 165 w
09-03 14:19:18.667 D/TVDIM: 29 h
09-03 14:19:18.668 D/TV2DIM: 29 w
09-03 14:19:18.668 D/TV2DIM: 29 h