![]() |
VOOZH | about |
Android ProgressBar is user interface control that is used to show some kind of progress. For instance, loading of some page, downloading of some file or waiting for some event to complete. In this article we will be discussing how to programmatically create a progress bar in Kotlin.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Navigate to app > res > layout > activity_main.xml and add a Progress bar and Button to show and hide the progress bar.
activity_main.xml:
Design UI:
Navigate to app > src > main > java > {package-name} > MainActivity.kt.
In this file, we will declare a variable progressBar to create the ProgressBar widget
val progressBar = ProgressBar(this)
progressBar.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
then add the widget in layout
val layout: LinearLayout = findViewById(R.id.layout)
layout.addView(progressBar)
MainActivity.kt: