Wednesday, February 20, 2013

Android Custom seekBar

You can download the source code from CustomSeekBar

The seekBar is an essential component which we are using on our application in customized way.

please make a demo project and follow the following code to yours mainactivity

SeekbarActivity.java



package com.example.seekbarwidget;


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

public class SeekbarActivity extends Activity { 
TextView textview; 
SeekBar seekbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seekbar); 
       
        textview = (TextView)findViewById(R.id.textView);
        seekbar = (SeekBar)findViewById(R.id.seekBar1);
        seekbar.setMax(10);
        seekbar.setProgress(5);
        //initControls();
        
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { 
         //add here your implementation 
            } 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { 
              //add here your implementation
            } 
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) { 

            textview.setText(" value = " +Integer.toString(progress));
            }
          });
    }

}


the activity_seekbar.xml is having following layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="125dp"
        android:indeterminate="false"
        android:max="10"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:progress="5"
        android:progressDrawable="@drawable/styled_progress"
        android:secondaryProgress="5"
        android:thumb="@drawable/thumbler_small" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Show seekbar value"
        android:textColor="#CD2134"
        android:textSize="27px"
        android:textStyle="bold" />

</LinearLayout>

finally the  styled_progress.xml inside drawable folder is having the lines

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/SecondaryProgress"
        android:drawable="@drawable/progress_cyan"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/progress_red"/>

</layer-list>


please use the following resources as drawable





The output is as shown below 



1 comment:

  1. Could you please tell me how can i implement seekbar something like AndroVid app, where you can select two time stamps of a video, used to clip a video.
    Example : https://play.google.com/store/apps/details?id=com.androvid
    Thanks in advance.

    ReplyDelete