'Animating a label (with TransitionManager)

I am writing a small Android app and am now facing this basic animation challenge. I have a label positioned at the top-left of the display and I want to move it to the bottom-right using an animation taking about one second. This must be easy, but since this is my first Android app, I don't know yet how to do it. My current code is below. It is important for me that the label is generated programmatically. I have already looked on the net and think that I should probably use TransitionManager, but I am not sure and have not yet been able to do anything working at this point.

val constraintLayout = findViewById(R.id.main)
val frameID = resources.getIdentifier("main","id",packageName)
val constrSet = ConstraintSet()
val label = TextView(this)
label.text = "Hello Android-World"
constraintLayout?.addView(label)
constrSet.clone(constraintLayout)

// Layout the label inside the frame, at the top-left:
constrSet.connect(label.id, ConstraintSet.LEFT, frameID, ConstraintSet.LEFT)
constrSet.connect(label.id, ConstraintSet.TOP, frameID, ConstraintSet.TOP)
constrSet.setMargin(label.id, ConstraintSet.LEFT,30)
constrSet.setMargin(label.id, ConstraintSet.TOP,30)
constrSet.applyTo(constraintLayout)

return; // Stop execution here.

/* I want to know how to smoothly animate the view to move from the previous layout at the top-left to the next one at the bottom-right. */

// The following code would layout the label inside the frame, at the bottom-right:
constrSet.connect(label.id, ConstraintSet.RIGHT, frameID, ConstraintSet.RIGHT)
constrSet.connect(label.id, ConstraintSet.BOTTOM, frameID, ConstraintSet.BOTTOM)
constrSet.setMargin(label.id, ConstraintSet.RIGHT,30)
constrSet.setMargin(label.id, ConstraintSet.BOTTOM,30)
constrSet.applyTo(constraintLayout)

.... After doing some researching and experimenting. I found out that the code hereafter may almost be one solution for my problem:

val mainPanel = findViewById<ConstraintLayout>(frameID)
val xv = mainPanel.width - 30
val yv = mainPanel.height - 30
label.animate()
    .x(xv.toFloat())
    .y(yv.toFloat())
    .setDuration(1000)
    .start()

But the precise setting of the values for xv and yv is still an issue. Even though I make an animation, it does note exactly match the condition represented in the bottom right constraint I was aiming at.

I also tried some other variation like:

val xv = mainPanel.width - 30 - label.with
val yv = mainPanel.height - 30 -label.height

But it did not work.

Any relevant tip would be welcome.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source