I am trying to make an invisible button unclickable until it is visible. Button is instantiated as such:
val button1 = findViewById<Button>(R.id.button1 )
button1.setOnClickListener {
val intent = Intent(this, ActivityClass::class.java)
startActivity(intent)
}
hide(button1)
hide util function is as follows:
@RequiresApi(Build.VERSION_CODES.Q)
fun hide(v: View?) {
v?.setTransitionVisibility(View.INVISIBLE)
}
The button becomes visible via an onTouch event that triggers a util function called fade that uses View.startAnimation to display elements (tapMe is a ScrollView that spans the entire screen width/height=match_parent):
tapMe.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
button1.fadeIn(1000)
tapMe.setTransitionVisibility(View.INVISIBLE)
}
}
return v?.onTouchEvent(event) ?: true
}
})
fadeIn util function is as follows:
@RequiresApi(Build.VERSION_CODES.Q)
fun View.fadeIn(durationMillis: Long = 250) {
this.startAnimation(AlphaAnimation(0F, 1F).apply {
duration = durationMillis
startOffset = delay
fillAfter = true
})
this.setTransitionVisibility(View.VISIBLE)
}
You ca use the
AnimatorListenerAdapterofObjectAnimatorthat providesonAnimationStart()andonAnimationEnd()listeners.Create an extension function on
ObjectAnimator.Use
ObjectAnimatorto fade in your view.