Android SeekBar

A SeekBar is a child class of ProgressBar that adds a draggable thumb. The user can hold the thumb and drag right or left to set the current progress level. SeekBar.OnSeekBarChangeListener  is used to notify of the user's actions.

You can download source code here.

Create Android Project with name as SeekBarDemo with package name "com.etr.seekbardemo". Create Activity with name as  SeekBarActivity. This is the code for SeekBarActivity.

package com.etr.seekbardemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarActivity extends Activity {
SeekBar seekBar;
TextView progress;
int value;

@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seek_bar);

progress = (TextView) findViewById(R.id.textView1);

seekBar = (SeekBar) findViewById(R.id.seekBar1);

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
progress.setText("SeekBar position is " + value);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Toast.makeText(SeekBarActivity.this, "StartTracking",
// Toast.LENGTH_LONG).show();
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
value = progress;
// Toast.makeText(SeekBarActivity.this,
// "SeekBar onProgressChanged  "+progress,
// Toast.LENGTH_LONG).show();
}
});

} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seek_bar, menu);
return true;
}

}

This is code for activity_seek_bar.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=".SeekBarActivity" >

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="set progress" />

</RelativeLayout>

Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService