'Event synchronization issue in an Android app

Here is an issue I am facing in an Android app. Please let me know if I need to give more information.

There is an animation running inside the app. And while it is running I can push a button which causes the following chunk of code to be executed:

......
randomNumber?.also {
    it.setTextColor(Color.rgb(0xFF,0x00,0x00))
}

btnLabel.setTextColor(Color.rgb(0xFF,0x00,0x00))
missCount += 1
setFixIndication()
......

Finally, this is the problem puzzling me.

The lines of the code containing .setTextColor(Color.rgb(0xFF,0x00,0x00)) are executed as soon as the button is touched, without waiting the end of the animation. This is what I expect.

But the code inside the called function setFixIndication() is only executed after the animation is over. This is not what I want.

In case this may be useful, I can say that the setFixIndication() function is just changing the position of some view in the layout.

Does anyone has a tip on how to handle the problem or where to look at?

Just in case this may be useful here is the code for the setFixIndication() function:

   fun setFixIndication() {
        if (fixPanel == null) {
            fixPanel = ImageView(this)
            fixPanel?.id = View.generateViewId()
            fixPanel?.setImageResource(R.drawable.ic_2p_fix)
            fixPanel?.elevation = (-1).dpToPixels(this)
            constraintLayout?.addView(fixPanel)
        }

        val constrSet = ConstraintSet()
        val anchor = 38
        val shift = 180
        constrSet.clone(constraintLayout)

        val btnIdx = get2Expo(randomNumber?.text.toString())
        val btnID = resources.getIdentifier('N'.plus(btnIdx.toString()),"id",packageName)
        val shfMult = 3 - (btnIdx - btnIdx % 8) / 8

        fixPanel?.also {
            constrSet.connect(it.id, ConstraintSet.LEFT, btnID, ConstraintSet.LEFT)
            constrSet.connect(it.id, ConstraintSet.RIGHT, btnID, ConstraintSet.RIGHT)
            constrSet.connect(it.id, ConstraintSet.TOP, btnID, ConstraintSet.TOP)
            constrSet.connect(it.id, ConstraintSet.BOTTOM, btnID, ConstraintSet.BOTTOM)
            constrSet.setMargin(it.id, ConstraintSet.TOP,anchor-shift*shfMult)
        }
        constrSet.applyTo(constraintLayout)
    }

Here is one more thing I noticed. If instead of using setFixIndication() I use another function setFixShortIndic() like the following one:

fun setFixShortIndic() {
    val btnIdx = get2Expo(randomNumber?.text.toString())
    val btnLabel = findViewById<TextView>(exponArray[btnIdx])
    btnLabel.setTextColor(Color.rgb(0x99,0xFF,0x00))
}

This last function only changes the text color of a label.

Then my problem disappears and the setFixShortIndic() function executes without waiting for the end of the animation.

This proves that the issue is connected to something I do inside the setFixIndication() function.

That said I still do not know how to solve the problem.



Sources

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

Source: Stack Overflow

Solution Source