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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.andexample.MainActivity" >
<SeekBar
        android:id="@+id/seek1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:max="200"
        android:progress="100"
        
        
        />
  <ImageView
     android:id="@+id/imgview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/seek1"
     />

</RelativeLayout>


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.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
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) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.angkor);
     imgView=(ImageView)findViewById(R.id.imgview);
     imgView.setImageBitmap(bitmap);
     seekBar= (SeekBar) findViewById(R.id.seek1);
     seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
imgView.setColorFilter(setBrightness(progress));     
}

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);

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

}

Android-SeekBar-example

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.