SeekBar Android Example Code and Tutorial

Code To Demo A ProgessBar That Can Be Dragged

Understand how the SeekBar is used in an Android App. This tutorial provides some SeekBar Android example code for you to try out. This demo code for the SeekBar appears in the API Demos App that comes with the Android Software Development Kit (SDK). The API Demos App is very large and it can be difficult to find the sample code you need. Putting the SeekBar Android example here allows it to be easily referenced.

The SeekBar is a useful User Interface (UI) control. It allows a user to easily vary an integer value simply by dragging left or right. Although for very large integer values you may not have fine control and a EditText may be a better option. For small values, such as a percentage, it is a good solution for providing a value setting interface.

What Does an Android SeekBar Look Like?

The following two images compares the look of the SeekBar. The first shows it on older (original theme) devices, the second on recent Holo themed devices.

The Android SeekBar in action on older devices:

SeekBar Android Example Older Devices

The Android SeekBar in action on newer devices:

Seekbar Android Example Holo Devices

The SeekBar Android Example Code

Start by generating a new App in Eclipse, here it is called SeekBar Demo. (If you are new to Android programming see the articles Set Up Windows for Android Development and Your First Android Hello World Java Program.) You can of course add the SeekBar to an existing App by adapting the code appropriately. With the App open replace the contents of the MainActivity.javaactivity_main.xml and strings.xml files with the code shown below (keeping your chosen package name in the Java source code file). This code produces the screens shown above.

The Java code is stored in a file in the package under the src folder:

Here is the layout used for this sample. It is stored in activity_main.xml in the layout folder:

Three string values are added to strings.xml in the values folder. The full strings.xml now looks like this:

This simple SeekBar Android example should be enough to get you up and running with the SeekBar in your App. The App should run without any errors. If it fails to run examine the error messages to find the fault.

See the Android Developer page for the SeekBar class for more information.

Leave a Reply

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

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.