Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to set a counter in asynctask in android 4?
Posted
Updated 9-Aug-18 19:09pm

1 solution

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var tv=abc as TextView
        MyAsync(tv).execute()
    }

    class MyAsync(var x:TextView) : AsyncTask<String, String, String>() {
        var j=0
        override fun doInBackground(vararg params: String?): String {
            
           for(j in 0..3) {
               Thread.sleep(2000)
 // Calls onProgressUpdate()
               publishProgress(j.toString())
           }
            return j.toString()
        }

        override fun onProgressUpdate(vararg values: String?) {
            x.text=values[0]
        }
    }
 }
 
Share this answer
 
v2
Comments
Vikram Singh Negi 10-Aug-18 1:23am    
counter will run from 1 to 3 and display in textview.
this code is done in Kotlin

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900