How to Add RangeSeekbar in Android Using Kotlin?
Last Updated : 12 Sep, 2023
In this article, RangeSeekbar is implemented in an application in android. Android Seekbar is a type of progress bar. We can drag the seekbar from left to right and vice versa and hence changes the current progress. Here we use the RangeSeekbar library to add custom seekbar in our app. This library provides us various features like steps, mode, thumbDrawable, etc which makes it way better than seekbar provided by android.
Approach
Step 1: Add the support Library in your root build.gradle file (not your module build.gradle file). This library jitpack is a novel package repository. It is made for JVM so that any library which is present in github and bitbucket can be directly used in the application.
XML allprojects { repositories { maven { url 'https://jitpack.io' } } }
Step 2: Add the support library in build.gradle file and add the dependency in the dependencies section. Through this directly RangeSeekbar will be used in the XML.
XML dependencies { implementation 'com.github.Jay-Goo:RangeSeekBar:v3.0.0' }
Step 3: Create a string-array in strings.xml file present in the values folder.
strings.xml <string-array name="levelArray"> <item>Lv1</item> <item>Lv2</item> <item>Lv3</item> <item>Lv4</item> <item>Lv5</item> </string-array>
Step 4: Add the following code in the activity_main.xml file. In this file, seekbar is added to the layout and the important tag like steps, thumbDrawable, mode, and many more are added according to the requirement.
activity_main.xml <?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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="228dp" android:textSize="18sp" android:text="Set difficulty for problem:-" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.jaygoo.widget.RangeSeekBar android:id="@+id/range_seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:rsb_gravity="bottom" app:rsb_indicator_background_color="@color/colorProgress" app:rsb_indicator_height="30dp" app:rsb_indicator_width="50dp" app:rsb_mode="single" app:rsb_progress_color="@color/colorProgress" app:rsb_step_auto_bonding="true" app:rsb_step_color="@color/colorProgress" app:rsb_step_height="10dp" app:rsb_step_width="5dp" app:rsb_steps="4" app:rsb_thumb_drawable="@drawable/ic_baseline_brightness" app:rsb_tick_mark_layout_gravity="bottom" app:rsb_tick_mark_mode="other" app:rsb_tick_mark_text_array="@array/levelArray" app:rsb_tick_mark_text_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Add the following code in MainActivity.kt file. Here setOnRangeChangedListener is added with the seekbar. It is invoked when the user changes the seekbar bar and shows the percentage of progress to which it is changed.
MainActivity.kt package org.geeksforgeeks.rangeseekbar import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.jaygoo.widget.OnRangeChangedListener import com.jaygoo.widget.RangeSeekBar import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //whenever we change progress of seekbar this function //get invoked automatically. range_seekbar?.setOnRangeChangedListener(object : OnRangeChangedListener { override fun onRangeChanged( rangeSeekBar: RangeSeekBar, leftValue: Float, rightValue: Float, isFromUser: Boolean) { Toast.makeText(this@MainActivity, leftValue.toString(),Toast.LENGTH_LONG).show() } override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) { } override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) { } }) } }
Output:
Refer to the official documentation for more information.
Similar Reads
How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read
How to Create a Splash Screen in Android using Kotlin? Android Splash Screen is the first screen visible to the user when the applicationâs launched. Splash Screen is the user's first experience with the application that's why it is considered to be one of the most vital screens in the application. It is used to display some information about the compan
3 min read
How to Add a Range Slider in Android? In this article, we will be adding a Range Slider in Android. A Range Slider is used to select a value from a range of numbers. Mostly we see a Range Slider in the audio volume controller and screen brightness adjuster in Android devices. To add a range slider in our activity we will be using a thir
2 min read
How to add KenBurns View in Android? In this article, we will learn about how to add KenBurns View in android using java. KenBurns View is a useful library that is an extension of ImageView. It creates an immersive experience by animating its Drawable. We can use RandomTransitionGenerator to change the duration and acts as a interpolat
2 min read
How to Add Margin in Android using Jetpack Compose? In Android, Padding is used to offset the content of the view by a specific number of pixels from either direction, i.e., padding from left, right, top and bottom. Using Padding, we can create multiple borders to a view by applying a combination of multiple padding and border. Â So in this article,
2 min read