Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
A discrete SeekBar
is one that jumps in fixed steps rather than smoothly transitioning between values. Android added support for a discrete SeekBar
via the android:tickMark
attribute, which was introduced in API level 24.
Here's a guide on how to create a discrete SeekBar
in Kotlin:
Create a layout file, e.g., activity_main.xml
:
<SeekBar android:id="@+id/discreteSeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="10" <!-- maximum value --> android:progress="5" <!-- initial value --> android:tickMark="@drawable/tick_mark" />
Create a drawable resource for the tick mark, e.g., tick_mark.xml
in the res/drawable
directory:
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <size android:width="1dp" android:height="10dp" /> <solid android:color="#000" /> </shape>
In your MainActivity.kt
or corresponding Kotlin file:
import android.os.Bundle import androidx.appcompat.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.discreteSeekBar) seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { Toast.makeText(this@MainActivity, "Selected value: $progress", Toast.LENGTH_SHORT).show() } override fun onStartTrackingTouch(seekBar: SeekBar?) {} override fun onStopTrackingTouch(seekBar: SeekBar?) {} }) } }
If you're looking for more customization, there are third-party libraries like DiscreteSeekBar
which offer additional features for discrete SeekBars.
With the basic Android SDK, this approach gives you a discrete SeekBar with fixed intervals. If you need custom intervals or more advanced features, you'll either have to extend the base SeekBar
and implement custom behavior or use a third-party library tailored to your needs.
Implementing Discrete SeekBar example code in Kotlin:
Description: Discrete SeekBar is a SeekBar with discrete steps. Implement it in Kotlin by adding the DiscreteSeekBar library to your project and configuring it in the layout file or programmatically.
Code:
// Add DiscreteSeekBar library to your build.gradle implementation 'org.adw.library:discrete-seekbar:1.0.1' // In your activity or fragment val discreteSeekBar = findViewById<DiscreteSeekBar>(R.id.discreteSeekBar) // Set up DiscreteSeekBar discreteSeekBar.progress = 50 discreteSeekBar.setOnProgressChangeListener(object : DiscreteSeekBar.OnProgressChangeListener { override fun onProgressChanged(seekBar: DiscreteSeekBar?, value: Int, fromUser: Boolean) { // Handle progress change } override fun onStartTrackingTouch(seekBar: DiscreteSeekBar?) { // Handle start tracking touch } override fun onStopTrackingTouch(seekBar: DiscreteSeekBar?) { // Handle stop tracking touch } })
Setting up Discrete SeekBar in XML layout using Kotlin:
Description: Add Discrete SeekBar to your XML layout file using the following code snippet:
XML Layout:
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar android:id="@+id/discreteSeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" app:dsb_max="100" app:dsb_min="0" app:dsb_progress="50" app:dsb_trackColor="@color/colorPrimary" app:dsb_thumbColor="@color/colorAccent"/>