Skip to content

Instantly share code, notes, and snippets.

@shubham836
Created August 29, 2021 17:08
Show Gist options
  • Save shubham836/3d2804cb9d1f27156819240581f0e10a to your computer and use it in GitHub Desktop.
Save shubham836/3d2804cb9d1f27156819240581f0e10a to your computer and use it in GitHub Desktop.
package com.example.livedatatest
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.*
import kotlinx.coroutines.runBlocking
import java.util.*
//import implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
//import implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
class MainActivity : AppCompatActivity() {
lateinit var editText:EditText
lateinit var editText2:EditText
var number=0
private lateinit var timer: Job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText=findViewById<EditText>(R.id.editText)
editText2=findViewById<EditText>(R.id.editText2)
var star="*"
editText.addTextChangedListener {
it!!.length
// runBlocking {
// delay(1000L)
// editText2.setText("*")
// }
lifecycleScope.launch(Dispatchers.Main) {
var tempString=""
for(times in 1..it.length){
tempString += "*"
}
delay(500L)
editText2.setText(tempString)
}
// Timer().schedule("this",1000L)
}
timer= startCoroutineTimer(delayMillis = 0, repeatMillis = 100) {
number++
Log.i("Timer ran","running $number")
if (number==100)
timer.cancel()
}
// timer.cancel()
}
private inline fun startCoroutineTimer(delayMillis: Long = 0, repeatMillis: Long = 0, crossinline action: () -> Unit) = GlobalScope.launch {
delay(delayMillis)
if (repeatMillis > 0) {
while (true) {
action()
delay(repeatMillis)
}
} else {
action()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment