Home > Back-end >  How to show animated 3 dot progress one by one in each second in textview in Android
How to show animated 3 dot progress one by one in each second in textview in Android

Time:01-18

This is my xml code

<LinearLayout
        android:id="@ id/progress_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@ id/tv_title"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@ id/tv_progrees"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Please wait"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <TextView
            android:id="@ id/tv_dot_progrees"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..."
            android:textColor="@color/white"
            android:textSize="24sp" />
    </LinearLayout>

This is my function to display progress dot in animated one by one in textview

 var defaultDot = "."
            Handler(Looper.getMainLooper()).postDelayed({
                tv_dot_progrees.text =   "."
                defaultDot = if(defaultDot.length == 3){
                    ""
                }else{
                    "$defaultDot."
                }
            }, 1000)

i am unable to see progress animated on by one please help me what i am doing wrong I want see each dot (.) one by one once its reach to 3 then again its start from one . enter image description here

CodePudding user response:

try this one. fits directly to the requirement

        var defaultDot = "."
        val customHandler = Handler(Looper.getMainLooper())
        val runnable: Runnable = object : Runnable {
            override fun run() {
                binding.tvDotProgrees.text = "$defaultDot"

                defaultDot = if (defaultDot.length == 3) {
                    ""
                } else {
                    "$defaultDot."
                }
                customHandler.postDelayed(this, 1000)
            }
        }
        runnable.run()

CodePudding user response:

Your Handler process will just run once after 1 second and stop afterwards.

If you want your Handler process to run repeatedly, you need something like this:

val repeatJob = object : Runnable {
    override fun run() {
        tv_dot_progrees.text = "."
        defaultDot = if (defaultDot.length == 3) {
            ""
        } else {
            "$defaultDot."
        }
        Handler(Looper.getMainLooper()).postDelayed(this, 1000)
    }
}
repeatJob.run()

And if you would like to stop the Handler process afterwards, you probably want to define your Handler, and later call Handler.removeCallbacksAndMessages(null) or Handler.removeCallbacks(Runnable).

  • Related