Open top menu


Android seekbar is a kind of Progress bar android widgets .  the seek bar can drag thumb left to right move for example - used seekbar in media player android application, the user move song back or forward via seekbar.

the SeekBar.OnSeekBarChangeListener interface provided this method.


activity_main.xml
...........................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     
        android:gravity="center"
        android:text="Android Beginner tutorials"
        android:textSize="12dp" />
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:gravity="center">
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

 
    </LinearLayout>

</LinearLayout>




MainActivity.java
...........................................................................................................................................................


package com.androidbeginnerpoint.testapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
private SeekBar seekBar;
private TextView textView;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("SeekBar tutorial");
setContentView(R.layout.activity_main);

seekBar = (SeekBar) findViewById(R.id.seekBar1);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("Covered: " + seekBar.getProgress() + "/"
+ seekBar.getMax());

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

int progress = 0;

@Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {

progress = progresValue;

Toast.makeText(getApplicationContext(),
"Changing seekbar's progress", Toast.LENGTH_SHORT)
.show();

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

Toast.makeText(getApplicationContext(),
"Started tracking seekbar", Toast.LENGTH_SHORT).show();

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

textView.setText("Covered: " + progress + "/"
+ seekBar.getMax());

Toast.makeText(getApplicationContext(),
"Stopped tracking seekbar", Toast.LENGTH_SHORT).show();

}

});

}
}

0 comments