Android Horizontal ProgressBar Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn how to use horizontal progressBar using kotlin in any android application. We will also learn how to add horizontal progressBar in linearLayout programmatically in any android application.

Output

Tutorialwing Android Horizontal ProgressBar  Output Android horizontal progressbar in kotlin

Tutorialwing Android Horizontal ProgressBar Output

Getting Started

Android Horizontal progressBar can be defined as below –

Android ProgressBar is an user interface that represents progress of an operation. Here, Horizontal ProgressBar means progressBar as a horizontal line.

Different Attributes of Android Horizontal ProgressBar Widget

We have already covered attributes of horizontal progressBar in ProgressBar Widget Tutorial. Please this post to know more about different attributes.

Example of Android Horizontal ProgressBar Using Kotlin

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ProgressBarHorizontal. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Now, we will modify xml and other file to use horizontal progressBar using kotlin in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use Horizontal ProgressBar Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<ProgressBar
		android:id="@+id/progressBar"
		style="?android:attr/progressBarStyleHorizontal"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:indeterminate="true"
		android:layout_margin="20dp"/>

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Hide Progressbar"/>

</LinearLayout>

In acitivity_main.xml file, we have defined a linearLayout and button. Button will be used to show/hide progressBar when it is clicked. Notice the styling style=”?android:attr/progressBarStyleHorizontal” in ProgressBar widget. This attribute is responsible for displaying progressBar horizontally.

4. Access Horizontal ProgressBar Widget using Kotlin file

Open src/main/java/com.tutorialwing.progressbarhorizontal/MainActivity.kt file and add below code into it.

package com.tutorialwing.progressbarhorizontal

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.ProgressBar

class MainActivity : AppCompatActivity() {

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

        val progressBar = findViewById<ProgressBar>(R.id.progressBar)
        if (progressBar != null) {
            val btn = findViewById<Button>(R.id.button)
            btn?.setOnClickListener {
                val visibility = if (progressBar.visibility == View.GONE) View.VISIBLE else View.GONE
                progressBar.visibility = visibility

                val btnText = if (progressBar.visibility == View.GONE) "SHOW PROGRESSBAR" else "HIDE PROGRESSBAR"
                btn.text = btnText
            }
        }
    }
}

Here, we have accessed button and horizontal progressBar using kotlin file (in MainActivity.kt). After that, we set a click listener on button to show / hide horizontal progressBar.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.progressbarhorizontal"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android Horizontal ProgressBaar using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners