fastread homefastrread library fastread menu

Android : SeekBar

Tutorial by:      Date: 2016-05-10 05:04:56

❰ Previous Next ❱

Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.

The SeekBar.OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.

Android SeekBar and RatingBar classes are the sub classes of AbsSeekBar.

 

Android SeekBar Example

activity_main.xml

Drag the seek bar from the pallete, now activity_main.xml will look like this:

File: activity_main.xml
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <SeekBar  
  12.         android:id="@+id/seekBar1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginTop="39dp" />  
  18.   
  19. </RelativeLayout>  

Activity class

Let's see the Activity class displaying seek bar and performing event handling.

File: MainActivity.java
 
  1. package com.example.seekbar;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.SeekBar;  
  6. import android.widget.SeekBar.OnSeekBarChangeListener;  
  7. import android.widget.Toast;  
  8. public class MainActivity extends Activity implements OnSeekBarChangeListener{  
  9.     SeekBar seekBar1;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.           
  15.         seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
  16.         seekBar1.setOnSeekBarChangeListener(this);  
  17.     }  
  18.     @Override  
  19.     public void onProgressChanged(SeekBar seekBar, int progress,  
  20.             boolean fromUser) {  
  21.         Toast.makeText(getApplicationContext(),"seekbar progress: "+progress, Toast.LENGTH_SHORT).show();  
  22.     }  
  23.     @Override  
  24.     public void onStartTrackingTouch(SeekBar seekBar) {  
  25.         Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();  
  26.     }  
  27.     @Override  
  28.     public void onStopTrackingTouch(SeekBar seekBar) {  
  29.         Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
  30.     }  
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37. }  

❰ Previous Next ❱