Reputation: 119
I am trying to build a constraint layout programmatically and I have to be able to rearrange the pieces of the layout at will. I'm placing all the layouts into the constraint layout and creating the guidelines so that each view only takes a certain percentage. I've posted the code for the constraint layout stuff below, but basically after I add a view, I create and add the guideline that goes with it. After that, I put the parameters for the 'connect' method in ConstraintSet in a list because it was my understanding that all the views needed to be added before doing so. After all the views are added, I clone the ConstraintSet from the ConstraintLayout, and then iterate through my connect param list connecting all my views...but that doens't work.
//mConstraintSets holds all info needed to connect the views later
protected Guideline createGuideline(){
Guideline guideline = new Guideline(mContext);
mConstraintSets.add(new ManifestConstraintSet(guideline.getId(), true));
manifestLayout.addView(guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
params.guidePercent = layoutPercent; // 45% - range: 0 <-> 1
guideline.setLayoutParams(params);
return guideline;
}
//This will create the constaint for the toolbar
protected void constraintToolbar(int viewID){
aboveViewID = viewID;
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0));
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0));
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0));
}
//This will create the constraint for a view
protected void constraintView(int viewID, float percent){
layoutPercent += percent;
Guideline guideline = new Guideline(mContext);
Boolean isBottomView = false;
//Check if the view is the bottom view
//if so don't create a guideline
if(layoutPercent < 1f){
guideline = createGuideline();
}else{
isBottomView = true;
}
//region left, right, & top constraints are the same for every view
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0));
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0));
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.TOP, aboveViewID, ConstraintSet.BOTTOM, 0));
aboveViewID = viewID;
//endregion
if(isBottomView){
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0));
}else{
mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.BOTTOM, guideline.getId(), ConstraintSet.TOP, 0));
}
}
protected void addConstraintsToSet(){
manifestConstraintSet.clone(manifestLayout);
for (ManifestConstraintSet set:mConstraintSets) {
if(set.isGuideline){
manifestConstraintSet.create(set.startView, ConstraintSet.VERTICAL_GUIDELINE);
}else{
manifestConstraintSet.connect(set.startView, set.startPos, set.endView, set.endPos, set.margin);
manifestConstraintSet.constrainWidth(set.startView, ConstraintSet.MATCH_CONSTRAINT);
manifestConstraintSet.constrainHeight(set.startView, ConstraintSet.MATCH_CONSTRAINT);
}
}
manifestConstraintSet.applyTo(manifestLayout);
}
I don't think I have a complete understanding of how ConstraintLayouts really work programmatically. If it matters,when I create the views I am using static generate view id methods to set their ids.
Upvotes: 2
Views: 1351
Reputation: 62831
Are guidelines added like normal views in ConstraintLayout
? Take a look at create and setGuidelineBegin and other methods to set where a guideline should be positioned. I didn't see any guideline positions set in your code.
I would start with a very simple layout with a single TextView
(for example) and one guideline to make sure I understood the mechanics. I would then expand to the full layout.
Upvotes: 2