Android Discrete SeekBar Using Kotlin With Example

In this article, we will learn about android Discrete SeekBar using Kotlin. We will go through various example that demonstrates how to use different attributes of Discrete SeekBar. For example, …..

In this article, we will get answer to questions like –

  • What is Discrete SeekBar?
  • Why should we consider Discrete SeekBar while designing ui for any app?
  • What are possibilities using Discrete SeekBar while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Android Discrete Seekbar Output Android Discrete SeekBar Using Kotlin

Tutorialwing Android Discrete Seekbar Output

Getting Started

We can define android Discrete SeekBar widget 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.

Now, how do we use Discrete SeekBar in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as DiscreteSeekBar. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android Discrete SeekBar Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using Discrete SeekBar in Kotlin

Follow steps below to use Discrete SeekBar in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">DiscreteSeekBar</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <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"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    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.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.discreteseekbar
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.SeekBar
    import android.widget.Toast
    import com.tutorialwing.discreteseekbar.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupSeekBar()
    	}
    
    	private fun setupSeekBar() {
    		binding.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.

Now, run the application. We will get output as below –

Tutorialwing Android Discrete Seekbar Output Android Discrete SeekBar Using Kotlin

Tutorialwing Android Discrete Seekbar Output

Different Attributes of Discrete SeekBar in XML

Now, we will see how to use different attributes of Android Discrete SeekBar using Kotlin to customise it –

Set Id of Discrete SeekBar

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of Discrete SeekBar using android:id attribute like below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        />

Here, we have set id of Discrete SeekBar as discreteSeekBar_ID using android:id=”” attribute. So, if we need to reference this Discrete SeekBar, we need to use this id – discreteSeekBar_ID.
Learn to Set ID of Discrete SeekBar Dynamically

Set Width of Discrete SeekBar

We use android:layout_width=”” attribute to set width of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of Discrete SeekBar Dynamically

Set Height of Discrete SeekBar

We use android:layout_height=”” attribute to set height of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of Discrete SeekBar Dynamically

Set Padding of Discrete SeekBar

We use android:padding=”” attribute to set padding of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in Discrete SeekBar using android:padding=”” attribute.
Learn to Set Padding of Discrete SeekBar Dynamically

Set Margin of Discrete SeekBar

We use android:layout_margin=”” attribute to set margin of Discrete SeekBar.
We can do it as below –

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

Here, we have set margin of 10dp in Discrete SeekBar using android:layout_margin=”” attribute.
Learn to Set Margin of Discrete SeekBar Dynamically

Set Background of Discrete SeekBar

We use android:background=”” attribute to set background of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in Discrete SeekBar using android:background=”” attribute.
Learn to Set Background of Discrete SeekBar Dynamically

Set Visibility of Discrete SeekBar

We use android:visibility=”” attribute to set visibility of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of Discrete SeekBar using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Discrete SeekBar Dynamically

Set Text of Discrete SeekBar

We use android:text=”” attribute to set text of Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        />

Here, we have set text (“Hello Tutorialwing”) in Discrete SeekBar using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of Discrete SeekBar Dynamically

Set Color of Text in Discrete SeekBar

We use android:textColor=”” attribute to set color of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textColor="#ffffff"     
        />

Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in Discrete SeekBar using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of Discrete SeekBar Dynamically

Set Gravity of Discrete SeekBar

We use android:gravity=”” attribute to set gravity of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:gravity="center_horizontal"
        />

Here, we have set gravity of text in Discrete SeekBar using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of Discrete SeekBar Dynamically

Set Text in Uppercase, Lowercase

If we need to show text of Discrete SeekBar in uppercase or lowercase etc.

Set text in uppercase

We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="true"
        />

Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in Discrete SeekBar.

By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="false"
        />

Above code will set Hello Tutorialwing to Discrete SeekBar.

How do we set text in lowercase?

Answer –

  • In xml file – write all the text in lowercase.
  • In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to Discrete SeekBar.

Learn to Set Text in Uppercase or Lowercase Dynamically

Set Size of Text in Discrete SeekBar

We use android:textSize=”” attribute to set size of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textSize="20sp"
        />

Here, we have set size of text in Discrete SeekBar using android:textSize=”” attribute.
Learn to Set Size of Text of Discrete SeekBar Dynamically

Set Style (Bold/italic) of Text in Discrete SeekBar

We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textStyle="bold"
        />

Here, we have set style of text in Discrete SeekBar using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of Discrete SeekBar Dynamically

Set Letter Spacing of Text in Discrete SeekBar

We use android:letterSpacing=”” attribute to set spacing between letters of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:letterSpacing="1"
        />

Here, we have set spacing between letters of text in Discrete SeekBar using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of Discrete SeekBar Dynamically

Set Typeface of Text in Discrete SeekBar

We use android:typeface=”” attribute to set typeface in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:typeface="sans"
        />

Here, we have set typeface of text in Discrete SeekBar using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of Discrete SeekBar Dynamically

Set fontFamily of Text in Discrete SeekBar

We use android:fontFamily=”” attribute to set fontFamily of text in Discrete SeekBar.
We can do it as below –

    <SeekBar
        android:id="@+id/discreteSeekBar_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:fontFamily="sans-serif"
        />

Here, we have set fontFamily (Here, sans-serif) of text in Discrete SeekBar using android:fontFamily=”sans-serif” attribute.

Till now, we have see how to use android Discrete SeekBar using Kotlin. We have also gone through different attributes of Discrete SeekBar to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android Discrete SeekBar Widget

Below are the various attributes that are used to customise android Discrete SeekBar Widget. However, you can check the complete list of attributes of Discrete SeekBar in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

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

We have seen different attributes of Discrete SeekBar and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is Discrete SeekBar, how can we use android Discrete SeekBar using Kotlin ? etc. We also went through different attributes of android Discrete SeekBar.