I want to create a seekbar like the picture below in android(xml and kotlin).How? Also I need to do the kotlin portions in a custom adapter.
I tried using github libraries,but it seemed to be not working at all,or I am confused as to how to implement it in my project.
Please help or guide me to create a seekbar like in the picture.enter image description here
I have somthing like this from chatGPT,but I need to make it look like the picture above.HOw??
class SeekbarView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) {
private val waveColor = Color.BLUE
private val thumbColor = Color.RED
private val progressPath = Path()
private val thumbPath = Path()
private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
color = waveColor
}
private val thumbPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
color = thumbColor
}
private val thumbRect = RectF()
private var progress = 0f
private var maxProgress = 100f
init {
// Set up any additional initialization here
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val desiredWidth = suggestedMinimumWidth + paddingLeft + paddingRight
val desiredHeight = suggestedMinimumHeight + paddingTop + paddingBottom
val measuredWidth = measureDimension(desiredWidth, widthMeasureSpec)
val measuredHeight = measureDimension(desiredHeight, heightMeasureSpec)
setMeasuredDimension(measuredWidth, measuredHeight)
}
private fun measureDimension(desiredSize: Int, measureSpec: Int): Int {
val mode = MeasureSpec.getMode(measureSpec)
val size = MeasureSpec.getSize(measureSpec)
return when (mode) {
MeasureSpec.EXACTLY -> size
MeasureSpec.AT_MOST -> desiredSize.coerceAtMost(size)
MeasureSpec.UNSPECIFIED -> desiredSize
else -> desiredSize
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val width = width.toFloat()
val height = height.toFloat()
// Draw waveform
progressPath.reset()
val waveHeight = height / 2 // Height of the waveform
val waveStep = width / 100 // Width of each waveform segment
val centerY = height / 2 // Vertical center position
progressPath.moveTo(0f, centerY)
for (i in 0..50) {
val x = i * waveStep
val y = centerY + Math.sin(x / width * 2 * Math.PI + progress / maxProgress * 2 * Math.PI) * waveHeight
progressPath.lineTo(x, y.toFloat())
}
progressPath.lineTo(width, centerY)
progressPath.close()
canvas.drawPath(progressPath, progressPaint)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x
when (event.action) {
MotionEvent.ACTION_DOWN -> {
return true
}
MotionEvent.ACTION_MOVE -> {
val progress = x / width * maxProgress
setProgress(progress.coerceIn(0f, maxProgress))
invalidate()
return true
}
MotionEvent.ACTION_UP -> {
// Handle thumb release event here
return true
}
}
return super.onTouchEvent(event)
}
fun setProgress(progress: Float) {
this.progress = progress
invalidate()
}
fun setMaxProgress(maxProgress: Float) {
this.maxProgress = maxProgress
invalidate()
}
}
I used a github library https://github.com/massoudss/waveformSeekBar
And in gradle(app)- Paste this
In gradle(project) - Paste this
In xml file,use
Now if you want to use this seekbar to play audio file,Use the code below: