I'm doing some animations in android and I have players inside a soccer court. For some of the elements I want to add a label, add some constraints to group them with their respective view and when the animation starts have the labels sort of follow them around.
I've tried this two solutions but the textview keeps jumping to the origin of the constraint layout:
How to programmatically add views and constraints to a ConstraintLayout?
Add a view programmatically in ConstraintLayout
I've tried A LOT of variations for these solutions with no luck. I'm missing something of course...any thoughts would be greatly appreciated!
My code where I create the views:
//init imageview
ImageView imageView = new ImageView(this);
imageView.setId(View.generateViewId());
imageView.setTag(key);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//buscar el drawable correspondiente
imageView.setImageDrawable(setViewDrawable(vista));
imageView.setOnLongClickListener(this);
//set width and height of the view
Pair<Integer, Integer> widthAndHeight = getWidthAndHeight(key);
//set ancho y alto
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(widthAndHeight.first, widthAndHeight.second);
imageView.setLayoutParams(params);
mTacticLayout.addView(imageView);
//set position
if (!isEdit) {
Pair<Float, Float> xAndY = getViewXY(key);
Float rotation = getViewRotation(key);
//account for offsets
imageView.setX(xAndY.first - widthAndHeight.first / 2);
imageView.setY(xAndY.second - widthAndHeight.second / 2);
imageView.setRotation(rotation);
}
//HERE HERE HERE LOOK AT ME
if(imageView.getTag().toString().equals(r.getString(R.string.ball_tag))){
TextView textView = new TextView(this);
textView.setId(View.generateViewId());
textView.setText("My View");
mTacticLayout.addView(textView);
ConstraintSet set = new ConstraintSet();
set.clone(mTacticLayout);
set.connect(textView.getId(), ConstraintSet.TOP, imageView.getId(), ConstraintSet.BOTTOM, 0);
set.connect(textView.getId(), ConstraintSet.RIGHT, imageView.getId(), ConstraintSet.RIGHT, 0);
set.connect(textView.getId(), ConstraintSet.LEFT, imageView.getId(), ConstraintSet.LEFT, 0);
set.applyTo(mTacticLayout);
}