Recipe Using a Seek Bar

A seek bar is similar to a progress bar that can take user input to change the amount of progress. Current progress is indicated by a small sliding box called a thumb. A user can click and drag the thumb to visually indicate the new place to set the progress.The main activity is shown in Listing 4.23.

Listing 4.23 src/com/cookbook/seekbar/SeekBarEx.java package com.cookbook.seekbar;

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

public class SeekBarEx extends Activity {

private SeekBar m_seekBar;

boolean advancing = true;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

m_seekBar = (SeekBar) findViewById(R.id.SeekBar01); m_seekBar.setOnSeekBarChangeListener(new

SeekBar.OnSeekBarChangeListener() { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(fromUser) count = progress;

public void onStartTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) {}

Thread initThread = new Thread(new Runnable() { public void run() { show_time();

initThread.start();

int count;

private void show_time() {

for(count=0; count<100; count++) { m_seekBar.setProgress(count);

Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace();

The widget declaration in the layout XML file is shown in Listing 4.24. Note that rather than use the default thumb button, a cupcake image is used, as shown in Figure 4.15.

Listing 4.24 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"

android:textSize="24sp" android:text="Drag the cupcake" android:layout_alignParentTop="true" /> <SeekBar android:id="@+id/SeekBar01"

android:layout_centerInParent="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:thumb="@drawable/pink_cupcake_no_bg" /> </RelativeLayout>

Android Custom Seekbar
Figure 4.15 Seek bar with a custom picture of a cupcake as the thumb.
+1 0

Post a comment