Android SeekBar with Examples

Android SeekBar,In android, SeekBar is an extension of ProgressBar control with a draggable thumb. The SeekBar permit users to the touch the thumb and drag left or right to set the modern progress ranges.

The android SeekBar is a one of the useful UI element and it provide a consumer interface to pick the integer values inside the described variety.

Following is the pictorial representation of the use of a SeekBar in android packages.

By using draggable thumb in SeekBar we are able to slide left or right to select a fee among 0 and maximum integer fee which we described the use of android:max attribute. An example of SeekBar is our device Brightness manage or volume control.

In android, with the aid of the use of SeekBar.OnSeekBarChangeListener listener we are able to notify the client, when the progress level of seekbar has been modified.

Android SeekBar,Create Android SeekBar in XML Layout File

Android SeekBar,In android, we can create SeekBar in XML layout file using <SeekBar> element with different attributes like as shown below.Android SeekBar

<SeekBar android:id="@+id/seekBar1"
    android:layout_width=" wrap_content"
    android:layout_height="wrap_content"
    android:max="100"
    android:indeterminate="false"
    android:progress="50" />

If you observe above code snippet, we defined a seekbar (<SeekBar>) with different attributes, those are

Attribute Description
android:id It is used to uniquely identify the control
android:indeterminate It is used to show the cyclic animation in seekbar without an indication of progress.
android:max It is used to set the maximum value of seekbar.
android:progress It is used to set the default progress value between 0 and max. It must be an integer value.

In anroid, the SeekBar helps varieties of modes to show the progress, those are Determinate and Indeterminate.

Android SeekBar,Android SeekBar with Determinate Mode

Android SeekBar,Generally, we use the Determinate development mode in seekbar while we want to show the amount of progress has took place. For example, the share of file downloaded, quantity of records inserted into database, and many others.

Following is the instance which shows a Determinate seekbar that is 50% completes.

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50" />

The above code snippet will show the SeekBar like as shown below

Generally, when the progress value reaches 100 then the progress bar is full. By using android:max attribute we can adjust this value.

Android SeekBar with Indeterminate Mode

Generally, we use the Indeterminate progress mode in seekbar when we don’t know how long an operation will take or how a lot paintings has carried out.

In indeterminate mode the actual progress will no longer be shown, best the cyclic animation can be shown to signify that a few development is happing.

Following is the instance to set Indeterminate development mode in XML format report.

<SeekBar android:id="@+id/seekBar1"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:max="100"
    android:indeterminate="true"
    android:progress="0" />

The above code snippet will show the SeekBar like as shown below.

This is how we can define the Progress modes in SeekBar based on our requirements in android applications.

SeekBar Control Attributes

Following are the some of commonly used attributes related to SeekBar control in android applications.

Attribute Description
android:id It is used to uniquely identify the control
android:max It is used to specify the maximum value of the progress can take
android:progress It is used to specify default progress value.
android:background It is used to set the background color for progress bar.
android:indeterminate It is used to enable indeterminate progress mode.
android:padding It is used to set the padding for left, right, top or bottom of progress bar.
android:progressDrawable It is used to set the custom drawable xml for the progress mode of a seekbar.
android:thumb It is used to set the thumb icon on seekbar to drag left or right.

SeekBar Control Example

Following is the instance of defining a SeekBar control and TextView manipulate in RelativeLayout to get the progress adjustments in seekbar the usage of SeekBar modified listener event.

Create a brand new android utility the use of android studio and deliver names as SeekBarExample. In case if you aren’t aware about growing an app in android studio test this text Android Hello World App.

Now open an activity_main.Xml report from reslayout route and write the code like as shown below

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="200dp"
        android:max="100"
        android:indeterminate="false"
        android:progress="0" />
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/seekBar1"
        android:layout_below="@+id/seekBar1"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="130dp"
        android:textSize="20dp"
        android:textStyle="bold"/>
</RelativeLayout>

If you observe above code we created a one SeekBar manipulate and one TextView control in XML Layout record.

Once we’re done with creation of format with required controls, we need to load the XML layout aid from our activity onCreate() callback technique, for that open fundamental interest file MainActivity.Java from javacom.Tutlane.Seekbarexample path and write the code like as shown under.

MainActivity.java

package com.tutlane.seekbarexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SeekBar sBar;
    private TextView tView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sBar = (SeekBar) findViewById(R.id.seekBar1);
        tView = (TextView) findViewById(R.id.textview1);
        tView.setText(sBar.getProgress() + "/" + sBar.getMax());
        sBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int pval = 0;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                pval = progress;
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            //write custom code to on start progress 
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                tView.setText(pval + "/" + seekBar.getMax());
            }
        });
    }
}

If you examine above code we are calling our format using setContentView approach within the shape of R.Layout.Layout_file_name in our interest report. Here our xml record name is activity_main.Xml so we used file name activity_main and we are trying to reveal the progress of task in are seeking bar on progress trade event.

Generally, in the course of the release of our activity, onCreate() callback approach might be known as by android framework to get the required format for an activity.

Output of SeekBar Example

When we run above example using android virtual device (AVD) we will get a result like as shown below.

If you look at above result, we’re capable of start showing the progress of assignment in are seeking for bar while we click on it in android application.

This is how we can use SeekBar manipulate in android programs to show the progress of project or work based on our necessities.