articles

Home / DeveloperSection / Articles / SeekBar in Android

SeekBar in Android

Nitish Kesarwani 3941 02-Jan-2018
Description

Android provides SeekBar control which is similar to progress bar except it has to drag and drop functionality by which user can adjust the functionality of the control. It is a part of widget category. Android studio provides a function to drag and drop the control in design interface so that developer can develop the app in quickly. We can use this control from palette bar.

 Controls like TextView and Seekbar are introduced in the program. These components are easily dragged and drop from palette toolbox and declare into the Main_Activity.xml. When controls are declared then you switch to Main_Activity.java file which contains control functionality performance. It is available in music player, volume adjustable interface and screen setting. SeekBar has OnprogressChanged, OnStartTrackingTouch and OnStopTrackingTouch method which is used check and tracks the user response.

Attributes

android:inderminate : it defines an indeterminate state of the seekbar. It means progress is not be identified.

android:drawable : we can define custom drawable screen for seekbar in xml file inside drawable directory.

android:thumb : Here, I have define custom drawable thumb in seekbar separately in xml file inside drawable directory.

android:max: In this attribute, we can define the maximum value of seekbar.

Android:progress : This attribute describe the default progress value of Seekbar.

Android:rotation : It is used to rotate the screen from the default value.

Methods

Seekbar.setSeekBarChangeListener(new SeekBarChangeListener()) : it notifies and check the current status of seekbar.

OnProgressChange(SeekBar sk,int progress,booleab fromUser) : It analyse the status of seekbar from initial level to current level.

onStartTrackingTouch(seekbar):-This method tell about the gesture event that handling the seekbar thumb is started by user.

onStopTrackingTouch(seekbar):-This method tell about the gesture event that handling the seekbar thumb is reached to maximum limit.

setProgess(): it defines default value of seekbar.

setMax() : It sets the maximum limit of seekbar

Android_mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.seekbar">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Main_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.seekbar.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:id="@+id/tV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SeekBar is Running"
        android:background="#199119"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#F9F9F9"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp">
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:max=”100”
        android:progress=”0”
        android:progressDrawable=”@drawable/progress”
        android:thumb =”@drawable/thumbnail”
        />
    </LinearLayout>
</LinearLayout>

Progress.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <gradient
        android:angle="270"
        android:endColor="@color/blue"
        android:startColor="@color/black" />
    <size
        android:height="35dp"
        android:width="35dp" />
</shape>

Thumbnail.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/seekbar"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@color/Yellow" />
    </item>
</layer-list>

Main_Activity.java
package com.mapsample.msclient009.seekbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
TextView tv;
SeekBar sb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        tv =(TextView)findViewById ( R.id.tV );
        sb =(SeekBar)findViewById ( R.id.seekbar );
        sb.setOnSeekBarChangeListener ( this );
    }
    public void onProgressChanged(SeekBar sb0,int p,boolean status)
    { if(p>10 && status==true) {
        onStartTrackingTouch ( sb0 );
        Toast.makeText ( getApplicationContext ( ), "progress is implemented"+p, Toast.LENGTH_LONG ).show ( );
    }else {onStopTrackingTouch ( sb0 );}
    }
    public void onStartTrackingTouch(SeekBar sb1)
    { Toast.makeText ( getApplicationContext (),"start Touching is Enabled",Toast.LENGTH_LONG ).show ();
    }
    public void onStopTrackingTouch(SeekBar sb2)
    {
        Toast.makeText ( getApplicationContext (),"Stop Seekbar is Enabled",Toast.LENGTH_LONG ).show ();
    }
}

SeekBar in Android SeekBar in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By