Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

SeekBar


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.     <SeekBar
  11.         android:id=“@+id/seekBar1”
  12.         android:layout_width=“match_parent”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_alignParentTop=“true”
  15.         android:layout_centerHorizontal=“true”
  16.         android:layout_marginTop=“39dp” />
  17. </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.         seekBar1=(SeekBar)findViewById(R.id.seekBar1);
  15.         seekBar1.setOnSeekBarChangeListener(this);
  16.     }
  17.     @Override
  18.     public void onProgressChanged(SeekBar seekBar, int progress,
  19.             boolean fromUser) {
  20.         Toast.makeText(getApplicationContext(),“seekbar progress: “+progress, Toast.LENGTH_SHORT).show();
  21.     }
  22.     @Override
  23.     public void onStartTrackingTouch(SeekBar seekBar) {
  24.         Toast.makeText(getApplicationContext(),“seekbar touch started!”, Toast.LENGTH_SHORT).show();
  25.     }
  26.     @Override
  27.     public void onStopTrackingTouch(SeekBar seekBar) {
  28.         Toast.makeText(getApplicationContext(),“seekbar touch stopped!”, Toast.LENGTH_SHORT).show();
  29.     }
  30.     @Override
  31.     public boolean onCreateOptionsMenu(Menu menu) {
  32.         // Inflate the menu; this adds items to the action bar if it is present.
  33.         getMenuInflater().inflate(R.menu.main, menu);
  34.         return true;
  35.     }
  36. }

Output:

android-seekbar-output1

android-seekbar-output2

Comments

comments