Below is a simple example of using SeekBar in an Android application using Java. The example includes a SeekBar and a TextView to display the progress:
There Are Some Common Attributes for SeekBar :
id | This ID serves as the control's unique identity. |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_centerVertical | true->Vertical Progress |
Step 1:
Layout Open your layout XML file (e.g., activity_main.xml) and add the following code to include a Switch widget:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
<TextView
android:id="@+id/progressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Progress: 0" />
</RelativeLayout>
Step 2:
File open com.microappvalley.newtestproject/MainActivity.kt file −:
package com.microappvalley.newtestproject
import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
private lateinit var seekBar: SeekBar
private lateinit var progressTextView: TextView
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Find the WebView by its id
seekBar = findViewById(R.id.seekBar)
progressTextView = findViewById(R.id.progressTextView)
// Set the maximum progress for the SeekBar (default is 100)
seekBar.max = 100
// Set a listener to track changes in SeekBar progress
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// Update the TextView with the current progress
progressTextView.text = "Progress: $progress"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// Not needed for this example
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// Not needed for this example
}
})
}
}
Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.