Here is a simple example to show how to use seek Bar in android.

Create a new project and place this code in it.

package com.coderzheaven;

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

public class SeekBarDemo extends Activity  implements SeekBar.OnSeekBarChangeListener {
    SeekBar mSeekBar;
    TextView mProgressText;
    TextView mTrackingText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSeekBar = (SeekBar)findViewById(R.id.seek);
        mSeekBar.setOnSeekBarChangeListener(this);
        mProgressText = (TextView)findViewById(R.id.progress);
        mTrackingText = (TextView)findViewById(R.id.tracking);
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        mProgressText.setText(progress + " " +
                getString(R.string.seekbar_from_touch) + "=" + fromTouch);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_on));
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_off));
    }
}

The main.xml file

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

    <SeekBar android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView android:id="@+id/progress"
       	android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/tracking"
       	android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The strings.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SeekBarDemo</string>
    <string name="seekbar_tracking_on">Tracking on</string>
    <string name="seekbar_tracking_off">Tracking off</string>
    <string name="seekbar_from_touch">from touch</string>
</resources>
SeekBar Demo in ANDROID

SeekBar Demo in ANDROID

Here is another example to show How to get the current progress on your SeekBar in android?

Please leave your valuable cmments

 

14 Comments

  1. ishan June 21, 2011 at 6:22 pm

    i want to know how to open camera and take picture and save that image to sd card uaing android..
    this is urgent .please nedd a help

     
    • James June 21, 2011 at 7:51 pm

      Ishan We will try………

       
    • shanthi November 21, 2011 at 4:25 pm

      first add camera uses permission in manifest file…and add external storage in manifest file ….this is important step for create the camera applications….

       
  2. ishan June 24, 2011 at 9:57 pm

    camara work is done.. 😀 i found it
    now i want to know how to decode json string

    [{“Status” : “Success”,”Values” : {“age” :25, “name” : “demo”, “address ” : “tokya”}]

    this is my string
    iwant to decode that please need a help

     
  3. Steve Cartoon July 26, 2011 at 3:09 am

    Perfect. This is exactly what I was looking for. The app I’m making requires six of these, three for variables, three for RGB settings. I suppose a little finagling with the math will allow me settings from 0-255.

    Thank you very much for this!

     
  4. Patrick Mahoney August 11, 2011 at 9:15 pm

    I’m trying to use a SeekBar and display the current progress of it below it in a TextView, but for some reason, when I try to use the getProgress() method, I get a weird force close error. Could you maybe show an example of doing that? Or is there an easier way?

    Thanks

     
    • James August 11, 2011 at 9:36 pm

      Hey Patrick:- What are you getting the reason for force close in the LogCat. Please paste it here, then I can help you.
      I tried it and I had no problem. or Put a try catch around it and print out the message inside catch.
      Anyway check the latest post for your problem.

       
  5. Pingback: How to get the current progress on your SeekBar in android? | Coderz Heaven

  6. Carol December 28, 2011 at 5:24 am

    Everything works fine in your demo UNTIL I drag the 1-2 TextViews *ABOVE* the SeekBar.

    Then the app crashes. Why would the “placement” matter in such a drastic fashion?

     
    • James December 28, 2011 at 6:24 am

      Hey Carol: Did you check the Logcat for the reason of crash. If yes please paste it here.

       
  7. Vera March 5, 2012 at 10:06 am

    Hey..this is the best !!! thanks <3

     
  8. Pingback: SeekBar Android « tediscript.wordpress.com

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.