skip to Main Content

I have problem with changing status in my progress bar on my app.
I have while loop which is changing status from 1-100 every 100ms.
Loop works cause when I’m making Log checker which shows how much is actually equals "I" it’s from 1-100.
When i’m putting manualy progress to for example 50 it shows but nothing change.

class StartActivity : AppCompatActivity() {

    private var progressBar: ProgressBar? = null
    private val handler = Handler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start)


        progressBar = findViewById<ProgressBar>(R.id.progressBar_activityStart)

        startProgressBar()
    }

    private fun startProgressBar() {
        progressBar!!.visibility = View.VISIBLE
        var i: Int = progressBar!!.progress
        progressBar!!.progress = 0

        Thread {
            while (i < 100) {
                i += 1
                handler.post {
                    progressBar!!.progress = i
                    Log.v("progress", i.toString())
                }
                try {
                    progressBar!!.progress = i
                    Thread.sleep(100)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
            progressBar!!.visibility = View.INVISIBLE
        }.start()
    }

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".StartActivity">

    <ImageView
        android:id="@+id/imageView_activityStart_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:layout_centerInParent="true"
        android:src="@drawable/logohorizontal" />

    <ProgressBar
        android:id="@+id/progressBar_activityStart"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_activityStart_logo"
        android:layout_alignBottom="@+id/imageView_activityStart_logo"
        android:layout_marginStart="30dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="30dp"
        android:layout_marginBottom="30dp"
        android:indeterminate="false"
        android:visibility="visible" />

</RelativeLayout>

2

Answers


  1. i had the same problem, then i changed

    <ProgressBar
            android:id="@+id/progressBar_activityStart"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" />
    

    to this:

    <android.widget.ProgressBar
            android:id="@+id/progressBar_activityStart"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" />
    

    then its worked. dont know what the problem is.

    Login or Signup to reply.
  2. You can try to use androidx.core.widget.ContentLoadingProgressBar.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search