Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up or turn off animation of RadioButton on Android?

I have a RadioGroup with dynamically added RadioButtons. In my Activity, I close the activity immediately after a RadioButton is selected. However, this leads to an artifact where two RadioButtons look like they are selected before the Activity finishes. The previous selection and current selection are both still animating when the Activity finishes.

Is there a way to speed up the animation or disable it altogether so that I don't see this visual artifact?

radioButton.setChecked(true);   // previous selected button is still deselecting and current button is now selecting. both are in the middle of animating but activity closes before they can finish animating  
finishWithResult(selected == null ? Activity.RESULT_CANCELED : Activity.RESULT_OK, selectedItem);
like image 761
VIN Avatar asked Nov 20 '22 07:11

VIN


1 Answers

In order to allow the user to see the selection of the RadioButton they've selected you can delay closing the Activity instead of turning off the RadioButton selection animation.

radioGroup.setOnCheckedChangeListener { radioGroup, checkedId ->
    Handler(Looper.getMainLooper()).postDelayed(
        {
            finish()
        },
        500
    )
}

If you would like to remove the animation regardless, you can see the answer here: https://stackoverflow.com/a/51411238/1212331

E.g.

radioGroup.setOnCheckedChangeListener { radioGroup, checkedId ->
    radioGroup.jumpDrawablesToCurrentState()
}
like image 181
caitcoo0odes Avatar answered Jun 01 '23 00:06

caitcoo0odes