0

How to wait at this point for 3 seconds //wait 3 sec then continue

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val textreg = AnimationUtils.loadAnimation(this, R.anim.textreg);
    val text1 = findViewById(R.id.text1) as TextView

    text1.startAnimation(textreg);
    //wait 3sec
    text1.visibility = View.GONE
}

}

Łukasz
  • 61
  • 3
  • 1
    dupe of [How to call a function after delay in Kotlin?](https://stackoverflow.com/questions/43348623/how-to-call-a-function-after-delay-in-kotlin) - you probably don't really want to 'wait' in the busy-waiting sense, but instead return and later cancel the animation/visibility after the timeout. – underscore_d Apr 30 '21 at 08:54
  • Just to explain if you're new to kotlin/android/ui development stuff - If you really decided to wait at the point where you put the comment, your UI would freeze for those three seconds - so you probably want to schedule a function call to happen after three seconds which is exactly what @underscore_d posted. – Dominik Apr 30 '21 at 09:05

2 Answers2

1

This probably answers your question How do you tell a function to wait a few seconds in kotlin

try this
Handler().postDelayed(Runnable { 
    //anything you want to start after 3s
    text1.visibility = View.GONE
}, 3000)
Somesh Bhalsing
  • 142
  • 1
  • 7
0

You can run a coroutine for example:

  GlobalScope.launch(Dispatchers.Main) {
        text1.startAnimation(textreg);
        delay(30000)
        text1.visibility = View.GONE
    }

Or you can make a animation. After 3 seconds the text view will start to dissapearing.

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="1"
    android:startOffset="3000"
    android:toAlpha="0" />
rogalz
  • 80
  • 8