Add setOnSeekBarChangeListener on Seekbar in android

How to implement on drag listener on seekbar in android programmatically.

setOnSeekBarChangeListener() is used to add different types of events on seekbar movement. For example you can easily change the number value by simply moving the seekbar in left or right side. So here is the complete step by step tutorial for Add setOnSeekBarChangeListener on Seekbar in android.

android-project-download-code-button

How to Add setOnSeekBarChangeListener on Seekbar in android.

Code for MainActivity.java file.

 package com.setonseekbarchangelistener_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {

 SeekBar seekbar;
 TextView textview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 seekbar = (SeekBar)findViewById(R.id.seekBar1);
 textview = (TextView)findViewById(R.id.textView1);
 
 
 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 textview.setText(" " + i);
 
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.setonseekbarchangelistener_android_examples.com.MainActivity" >

 <SeekBar
 android:id="@+id/seekBar1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:max="100" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/seekBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="25dp"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Add setOnSeekBarChangeListener on Seekbar in android

Click here to download Add setOnSeekBarChangeListener on Seekbar in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *