12 August 2015

Track SeekBar Progress in Android

What we will do ?
-> We will build a Seekbar in our application. A Toast will be used to get the value of the seekbar.

What we will need ?
  • SeekBar in activity_main.xml
  • MainActivity.java
Step 1 : Create a new application. Go to File->New->Android Application Project.
File New Android Project

Step 2 : Give a name to your application (SeekBarDemo) and package name (com.mia.seekbardemo).Click Next continuously.
seekbardemo mia
Click Finish.
finish

Step 3 : Drag SeekBar from Palette->Form Widgets in activity_main layout.
drag seekbar from palette
activity_main.xml
<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.mia.seekbardemo.MainActivity" >


    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        />
</RelativeLayout>

Step 4 : Open MainActivity.java from src->com.mia.seeekbardemo in Project Explorer and copy-paste the code shown below.

MainActivity.java
package com.mia.seekbardemo;

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

public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final SeekBar seek = (SeekBar) findViewById(R.id.seekBar1);
        
        seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
         
         int change = 0;
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    change=progress;  
   }

   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub 
   }

   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, Integer.toString(change), Toast.LENGTH_SHORT).show();
   }    
        });        
        }
    }

Step 5 : Run the application.
seekbar output

Explanation of the code :

Explanation from MainActivity.java :
  • We make a variable of class SeekBar named seek using findViewById() method which connects it to the widget in activity_main.xml with id as seekBar1.
final SeekBar seek = (SeekBar) findViewById(R.id.seekBar1);
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {      
         int change = 0;
  • Inside onProgressChanged() we give the value of progress variable defined by default in the method parameters to our variable change.
public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    change=progress;  
   }
  • Inside method onStopTrackingTouch() we define what should be done after touch action is carried out. We display a Toast with the value of change or progress which varies from 0 to 100 by default.
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, Integer.toString(change), Toast.LENGTH_SHORT).show();
   }   
Github download
Stay Tuned with Made In Android

Previous Page Next Page Home
Top