Native Mobile App Development With Java

Android SeekBar

15-Sep-2024

How to implement SeekBar in Android Studio Project with Java

Here is the tutorial for Kotlin



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_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_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.java file −:

Java:


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView progressTextView;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
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.setMax(100);

// Set a listener to track changes in SeekBar progress
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Update the TextView with the current progress
progressTextView.setText("Progress: " + progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Not needed for this example
}

@Override
public void 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.


Output:





Comments