Android SeekBar Using Kotlin With Example




Target

Hello Readers! In this post, we are going to learn about how to use android seekBar using kotlin in any android application. We will also learn about different attributes of android seekBar that can be used to customise this widget.

Output

Tutorialwing Android SeekBar Output Android SeekBar Using Kotlin

Tutorialwing Android SeekBar Output

Getting Started

Android SeekBar can be defined as below –

Android seekBar is an extension of progressBar that have draggable thumb. User can drag the thumb back and forth to set current progress value.

Note – Placing focusable widget to left or right of the seekBar widget is discouraged.

Different Attributes of Android SeekBar Widget

Some of the popular attributes of seekBar are –

Sr. XML Attributes Description
1 android:thumb It is used to draw a thumb on seekBar.

Some of the popular attributes of android seekBar inherited from AbsSeekBar are

Sr. XML Attributes Description
1 android:thumbTint It is used to set tint to apply on drawable of thumb.
2 android:thumbTintMode It is used to set blending mode to apply the thumb tint.
3 android:tickMarkTint It is used to set tint to apply on tick mark drawable.
4 android:tickMarkTintMode It is used to set blending mode used to apply the tick mark tint.

Some of the popular attributes of android seekBar inherited from ProgressBar are

Sr. XML Attributes Description
1 android:max Specifies the maximum value
2 android:min Specifies the minimum value
3 android:progress Specifies the default progress value (between 0 and max value)
4 android:progressDrawable Specifies drawable of the progress mode.

Some of the attributes of android seekBar inherited from View are –

Sr. XML Attributes Description
1 android:background Specifies background of the view
2 android:clickable Specifies whether view is clickable or not.
3 android:elevation Specifies base z-depth of the view
4 android:id Specifies id of the view.



Example of Android SeekBar Using Kotlin

At first, we will create an android application. Then, we will use seekBar using kotlin in this application.

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 SeekBar. 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.

Since we have a project now, we are going to change code inside xml and kotlin to use seekBar widget.

2. Modify values folder

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

3. Use SeekBar 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">

	<SeekBar
		android:id="@+id/seekBar"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="10dp"/>

</LinearLayout>

In activity_main.xml file, we have defined android seekBar widget. Now, we will access this seekbar using kotlin file to perform some operations on it.

4. Access SeekBar Widget in Kotlin file

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

package com.tutorialwing.seekbar

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.SeekBar
import android.widget.Toast

class MainActivity : AppCompatActivity() {

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

        val seekBar = findViewById<SeekBar>(R.id.seekBar)
        seekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                // Write code to perform some action when progress is changed.
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // Write code to perform some action when touch is started.
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // Write code to perform some action when touch is stopped.
                Toast.makeText(this@MainActivity, "Progress is " + seekBar.progress + "%", Toast.LENGTH_SHORT).show()
            }
        })
    }
}

In MainActivity.kt file, we have accessed seekBar widget. Then, we have set a listener, SeekBarChangeListener, to show a toast message whenever there is change in progress of the seekBar i.e. whenever you drag thumb of the
seekBar to Back and forth.

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

AndroidManifest.xml file

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.seekbar"
		  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 SeekBar using Kotlin.