Skip to content

Instantly share code, notes, and snippets.

@Gryzor
Created May 2, 2018 02:27
Show Gist options
  • Save Gryzor/a37202d49d3ded4f3a5f5e26166dc40f to your computer and use it in GitHub Desktop.
Save Gryzor/a37202d49d3ded4f3a5f5e26166dc40f to your computer and use it in GitHub Desktop.
public ProgressConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
configureViews();
}
private void configureViews() {
// Create a Constraint Set and clone our constraints
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(this);
// Create our progress bar…
ProgressBar progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setId(generateViewId());
// add it to our hierarchy
addView(progressBar, new ConstraintLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// Configure its constraints … basically pin it to every side.
constraintSet.connect(progressBar.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
constraintSet.connect(progressBar.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
constraintSet.connect(progressBar.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
constraintSet.connect(progressBar.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
// now apply the new constraints (This will not work, by the way) ;)
constraintSet.applyTo(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment