MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets reference from XML layout
val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)
val progressBar = findViewById<ProgressBar>(R.id.progressBar)
// Variable to hold progress status
var progressStatus = 0;
// Initialize a new Handler instance
val handler = Handler(Looper.getMainLooper())
// Set a button click listener
button.setOnClickListener {
// Disable the button itself
it.isEnabled = false
Thread {
while (progressStatus < 100) {
// Update progress status
progressStatus += 1
// Sleep the thread for 100 milliseconds
Thread.sleep(100)
// Update the progress bar
handler.post {
progressBar.progress = progressStatus
textView.text = "$progressStatus"
}
}
}.start()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:background="#DCDCDC">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:padding="12dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textStyle="bold"
tools:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start Task"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>


- android kotlin - Draw text on bitmap
- android kotlin - Bitmap sharpen
- android kotlin - Bitmap black and white effect
- android kotlin - ImageView grayscale filter
- android kotlin - Bitmap invert colors
- android kotlin - ActionBar title text size
- android kotlin - ActionBar title color
- android kotlin - ActionBar title click listener
- android kotlin - ActionBar title center
- android kotlin - Popup menu example
- android kotlin - Context menu example
- android kotlin - Progressbar with percentage example
- android kotlin - Change ProgressBar color programmatically
- android kotlin - Change horizontal ProgressBar color
- android kotlin - selectableItemBackground programmatically