SEEKBAR
E:\android\MyApplication\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:max="10"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBar1"
android:layout_marginLeft="29dp"
android:layout_marginTop="14dp" />
</LinearLayout>
E:\android\MyApplication\app\src\main\java\com\example\myapplication\MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.widget.SeekBar
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var seekBar: SeekBar
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Enable edge-to-edge content
enableEdgeToEdge()
// Initialize views
seekBar = findViewById(R.id.seekBar1)
textView = findViewById(R.id.textView1)
// Initialize the text view with the initial progress
textView.text = "${seekBar.progress}/${seekBar.max}"
var progress = 0
// Set up the seek bar change listener
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progresValue: Int, fromUser: Boolean) {
//textView.text = "$progress/${seekBar?.max}"
progress = progresValue
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// Optional: Do something when tracking starts
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// Optional: Do something when tracking stops
textView.text = "$progress/${this@MainActivity.seekBar.max}"
}
})
}
}
|