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 .
Firstly, we need to create a project in Android Studio. To do follow these steps:
- Click on File, then New and then New Project and give name whatever you like
- Then, select Kotlin language Support and click next button.
- Select minimum SDK, whatever you need.
- Select Empty activity and then click finish.
Modify activity_main.xml file
Second step is to design our layout page. Here, we will use the RelativeLayout to get the ProgressBar from the Kotlin file.
Update the strings.xml file
Create ProgressBar in MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file , we declare a variable progressBar to create the ProgressBar widget like this
val progressBar = ProgressBar(this) //setting height and width of progressBar progressBar.layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
then add the widget in layout using this
val layout = findViewById
(R.id.layout) // Add ProgressBar to our layout layout?.addView(progressBar)