In this tutorial, we’ll discuss and implement SeekBar in our Android Application using Kotlin.

What is Android SeekBar?

SeekBar is a UI element that is an extension of the ProgressBar.

SeekBar adds a draggable thumb to the ProgressBar. It is commonly used in music apps to provide volume control.

SeekBar is like a scale with an upper and lower limit and every step is a single unit.

SeekBar XML Attributes

Some of the important XML attributes of SeekBar are:

  • android:minWidth/maxWidth/minHeight/maxHeight: these attributes are used to set the dimensions of the seekbar view. They don’t change the thickness of the SeekBar.
  • android:max/min: The upper/lower limit of the SeekBar. The android:min is available from Android SDK 26 and above.
  • android:progress: The current value of the thumb position.
  • android:progressTint: Here we pass the color for the progress to the left of the thumb position.
  • android:progressBackgroundTint: This color is displayed for the SeekBar background, to the right of the thumb.
  • android:thumb: Here we can pass a custom drawable that will act as the thumb of the seek bar.
  • android:thumbTint: color of the thumb.
  • android:thumbOffset: The distance between the thumb and the current progress in dp. A negative value shifts the thumb to the right of the progress. A positive one shifts it to the left.
  • style: it’s used to set custom/predefined styles on the SeekBar. There is a popular seekbar style – “Discrete” – which breaks the progress into discrete intervals.
  • android:tickMark: Here we pass a drawable, which acts as breakpoints on the SeekBar. The number of breakpoints/tickMarks is equal to android:max.
  • android:tickMarkTint: used to set a color on the tickMark drawable.
  • android:splitTrack: This expects a boolean value. By default on Android Lollipop and above this is true. It splits the Seekbar track into two parts – left and right of the SeekBar. This isn’t always visible in white background activities.

Creating SeekBar in Kotlin

We can implement the SeekBar.OnSeekBarChangeListener interface to create SeekBar programatically. We have to implement the following three Kotlin functions.

SeekBar Examples

Let’s look at the different types of SeekBar by setting the above properties.

1. Simple SeekBar

 

2. SeekBar with Progress Tint Color

 

3. SeekBar with Thumb Tint Color And Offset

 

4. SeekBar with TickMarks

The tickmark.xml drawable is given below.

5. SeekBar with Custom Style

Here we set Discrete style with different intervals (max value).

 

android-seekbar-style

6. SeekBar with Split Track

The first one has a Split track enabled by default. The second one does not.

Android SeekBar Kotlin Project Structure

android-seekbar-project-structure

1. Activity XML Code

The code for the activity_main.xml layout is as follows.

We’ve created a custom thumb in the last SeekBar.

custom_thumb.xml

2. Activity Kotlin Code

The Kotlin code for the MainActivity.kt class is as follows.

In the above code, we’ve created a SeekBar in Kotlin and added it to the LinearLayout.

We’ve set the SeekBar change listener on each of them. The p1 represents the current progress value which is the same as seekBar.getProgress(). We are setting this value in the TextView.

Output:

androidly seekbar app

By admin

Leave a Reply

%d bloggers like this: