Blog purpose for android basic example for android app developer. any query please my contact

Monday, 9 August 2021

Kotlin progress bar programmatically

 Kotlin progress bar programmatically

In this article, today learn  Kotlin progress bar programmatically. for more details   follow the link https://stackoverflow.com/questions/3548441/how-to-create-a-progressbar-programmatically


follow the code  Kotlin progress bar programmatically :


1.MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.widget.Button

import android.widget.ProgressBar

import android.widget.TextView

import android.os.Handler


class MainActivity : AppCompatActivity() {


private var progressBar: ProgressBar? = null

private var i = 0

private var txtView: TextView? = null

private val handler = Handler()

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

progressBar = findViewById<ProgressBar>(R.id.progress_Bar) as ProgressBar

txtView = findViewById(R.id.text_view) as TextView

val btn = findViewById(R.id.show_button) as Button

btn.setOnClickListener {

i = progressBar!!.progress

Thread(Runnable {

while (i < 100) {

i += 5

// Update the progress bar and display the current value

handler.post(Runnable {

progressBar!!.progress = i

txtView!!.text = i.toString() + "/" + progressBar!!.max

})

try {

Thread.sleep(100)

} catch (e: InterruptedException) {

e.printStackTrace()

}


}

}).start()

}

}


}



2.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent">


<ProgressBar

android:id="@+id/progress_Bar"

style="?android:attr/progressBarStyle"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_marginLeft="70dp"

android:layout_marginTop="150dp"

android:indeterminate = "true"

android:max="100"

android:minWidth="200dp"

android:minHeight="50dp"

android:progress="0" />


<TextView

android:id="@+id/text_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/progress_Bar"

android:layout_below="@+id/progress_Bar" />


<Button

android:id="@+id/show_button"

android:layout_width="191dp"

android:layout_height="wrap_content"

android:layout_below="@+id/text_view"

android:layout_marginLeft="70dp"

android:layout_marginTop="20dp"

android:text="Progress Bar" />

</RelativeLayout>

No comments:

Post a Comment