Android Discrete SeekBar Using Kotlin With Example




Target

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

Output

Tutorialwing Android Discrete Seekbar Output Android Discrete SeekBar Using Kotlin

Tutorialwing Android Discrete Seekbar Output

Getting Started

Android discrete seekBar can be defined as below –

Discrete seekBar is an extension of progressBar, with draggable thumb to set current progress, that works only for discrete values.

Different Attributes of Android Discrete SeekBar Widget

You can see our post on SeekBar widget to see list of popular attributes of discrete seekbar.

Example of Android Discrete SeekBar Using Kotlin

At first, we will create an android application. Then, we will use discrete seekBar widget in the 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 DiscreteSeekBar. 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 will modify xml and other files to use seekBar programmatically in kotlin file.

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"
		style="@style/Widget.AppCompat.SeekBar.Discrete"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:max="10"
		android:progress="3"/>

</LinearLayout>

In activity_main.xml file, we have defined seekBar widget. Notice the style style=”@style/Widget.AppCompat.SeekBar.Discrete”. This style is responsible for displaying the seekBar to work for discrete values. Now, we will access this seekBar widget into kotlin file to perform some action on it.

4. Access Discrete SeekBar using Kotlin file

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

package com.tutorialwing.discreteseekbar

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, "Current value is " + seekBar.progress, Toast.LENGTH_SHORT).show()
            }
        })
    }
}

In MainActivity.kt file, we have accessed discrete seekBar widget defined in xml file. Then, we have set a listener to show a toast message whenever there is a change in the progress of the seekBar i.e. whenever there is change in the position of the thumb of the seekBar.

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.discreteseekbar"
		  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 Discrete SeekBar using Kotlin.