Android SeekBar example

In this Android tip, I am going to show you how to use SeekBar widget. The SeekBar allows you to set a current progress value by touching or dragging the knop or thumb to the left or right. Typically, the minimum value of the SeekBar is zero. You can specify any positive number for its maximum value. To get the current value of the SeekBar, you have to implement setOnSeekBarChangeListener interface and override the onProgressChanged() method. In the example application below, the user can touch or drag the thumb of the SeekBar to change brightness of an image. To change the brightness of the image that is displayed in an ImageView, you will create instances of the PorterDuffColorFilter class and apply them to the ImageView using the setColorFilter() method. The maximum value of the SeekBar is 200. Its current value is 100. So, the thumb is in middle of the SeekBar. If the user drags the thumb to set the current progress value to be higher than 100, the image is brighter. Otherwise, the image is darker.

Now to have example application on using the SeekBar to set the brightness of the image, you create a new Android project. Then add a SeekBar and an ImageView to the activity_main.xml file.

<RelativeLayout xmlns:android=""
   tools:context="com.example.andexample.MainActivity" >


Then in the MainActivity class, you need to implement the OnSeekBarChangeListener interface to get the current progress set by the user. The current progress value is passed to the setBrightness() method. It is used to construct PorterDuffColorFilter objects for different levels of brightness.

package com.example.andexample;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar seekBar;
ImageView imgView;
Bitmap bitmap;
   protected void onCreate(Bundle savedInstanceState) {
     bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.angkor);
     seekBar= (SeekBar) findViewById(;
     seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){

public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

public void onStopTrackingTouch(SeekBar seekBar) {

   public static PorterDuffColorFilter setBrightness(int progress) {
   if (progress >= 100)
   int value = (int) (progress-100) * 255 / 100;

   return new PorterDuffColorFilter(Color.argb(value, 255, 255, 255), Mode.SRC_OVER);

   int value = (int) (100-progress) * 255 / 100;
   return new PorterDuffColorFilter(Color.argb(value, 0, 0, 0), Mode.SRC_ATOP);



Posted by: Dara | post date: 11-15-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.