Android Discrete SeekBar

Android Discrete SeekBar can be defined as below –

"Android Discrete SeekBar is an extension of ProgressBar that has a draggable thumb. User can drag the thumb back and forth to set the current progress of the seekbar. Discrete SeekBar works for discrete values."

You can see our tutorial on Android SeekBar to see list of popular attributes of discrete seekbar.

Now, At first, we will create an android application. Then, we will use discrete seekbar in the application.

 

 

 

 

Follow the steps below to create new project. Please ignore the steps if you have already created a new project.

S. No. Description
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as DiscreteSeekBar. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
4 Then, select Empty Activity => click next => click finish.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use discrete seekbar in the application.

<resources>
<string name="app_name">DiscreteSeekBar</string>
</resources> 

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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=".MainActivity">
 
<SeekBar
android:id="@+id/SeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.SeekBar.Discrete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:max="10"
android:progress="3"/>

</android.support.constraint.ConstraintLayout>

 

 

 

 

In activity_main.xml file, we have used android discrete seekbar. Note that we have used style=”@style/Widget.AppCompat.SeekBar.Discrete” style to use seekbar as discrete seekbar. Also, we have set maximum value for seekbar to 10. Attribute android:progress=”3″ is responsible for setting current progress of the discrete seekbar. Now, we will access this seekbar in java file.

Open src/main/java/com.ukacademe.discreteseekbar/MainActivity.java file. Then, add below code into it.

package com.ukacademe.discreteseekbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SeekBar seekBar = findViewById(R.id.SeekBar);
        if (seekBar != null) {
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    // Write code to perform some action when progress is changed.
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // Write code to perform some action when touch is started.
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // Write code to perform some action when touch is stopped.
                    Toast.makeText(MainActivity.this, "Current value is " + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
                }
            });
        }

    }
}

In MainActivity.java file, we have accessed seekbar. Then, we set listener, seekBarChangeListener, to show a toast message that display current progress of the discrete seekbar.

Since AndroidManifest.xml file is very important in any android application, we will also go through the code inside this file.

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.discreteseekbar">

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

When you run the program, you will get output as shown below.

UK Academe- DiscreteSeekBar-Output