Home > Enterprise >  Kotlin: Android Studio - Print on Emulator not the same as print using Log.v(TAG
Kotlin: Android Studio - Print on Emulator not the same as print using Log.v(TAG

Time:01-16

fun division(){
    val numerator = 1000   
    var denominator = 4
    var index = 1
    val outputText = ("Result is ${numerator / denominator}")
    val indexText: TextView = findViewById(Rid.index_textview)
    val divisionText: TextView = findViewById(R.id.division_textView)

    repeat(3){                                  //repeats block three times
        Log.v(TAG, "${numerator / denominator}")//logs output to Logcat
        divisionText.text= outputText           //outputs the calculation text to emulator
        indexText.text = ("Index is $index")    //outputs the index value text to emulator
        Thread.sleep(1000)//milliseconds        //pauses 1 second between repeats
        denominator--                           //decrements denominator by 1
        index                                   //increments index by 1
    }
}   

When the code (incomplete here) is run in Android Studio on the emulator, I get one line of text for each indexText and divisionText, instead of three expected. Additionally,the output for divisionText is 250 and the indexText is 3. The Android Studio logcat outputs each division calculation correctly. When I run the code in Kotlin playground, the code gives me three lines each of indexText and divisionText. However, the indexText value is output correctly, incrementing one more each repeat cycle, while the calculation of numerator / denominator only outputs the 'same' final calculation on each repeated line cycle.

CodePudding user response:

You have a sleep statement on the UI thread, which blocks the UI and prevents any update to the displayed values until it is all done. Use a coroutine with a delay instead to have the numbers update properly at a 1 second interval, like this:

fun division(){
    val numerator = 1000
    var denominator = 4
    var index = 1
    val indexText: TextView = findViewById(Rid.index_textview)
    val divisionText: TextView = findViewById(R.id.division_textView)

    lifecycleScope.launch {
        repeat(3) {
            Log.v(TAG, "${numerator / denominator}")
            divisionText.text= "Result is ${numerator / denominator}"
            indexText.text = "Index is $index"
            delay(1000) //milliseconds
            denominator--
            index  
        }
    }
}   

You also need to move your string creation to inside the loop so it updates properly each loop (before your outputText variable was constant, would not change when the variables got updated).

Note: You are also using integer division, so your result will be an integer, not a floating point number. If you want decimal output you need to convert the result to a double first, like this

val result = numerator / denominator.toDouble()
divisionText.text = "Result is $result"

or just change your numerator to a floating point type like this

val numerator = 1000.0
  •  Tags:  
  • Related