Hello Readers! In this post, we are going to learn about android discrete seekBar. We will also go through different attributes of discrete seekBar that can be used to customise it.
Output
Getting Started
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.
Attributes of Android discrete SeekBar Widget
You can see our post on SeekBar widget to see list of popular attributes of discrete seekbar.
Example of Android discrete SeekBar Widget
At first, we will create an android application. Then, we will use discrete seekBar in the application.
1. Creating New Project
Follow the steps below to create new project. Please ignore the steps if you have already created a new project.
Step | 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 17 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 post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use seekBar in the application.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">DiscreteSeekBar</string> </resources>
3. Use android discrete seekBar Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <SeekBar android:id="@+id/seekBar" style="@style/Widget.AppCompat.SeekBar.Discrete" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="10" android:progress="3"/> </LinearLayout>
In activity_main.xml file, we have used android discrete seekBar widget. Note that we have used style=”@style/Widget.AppCompat.SeekBar.Discrete” style to use seekBar widget 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 widget in java file.
4. Access discrete seekBar Widget in java file
Open src/main/java/com.tutorialwing.discreteseekbar/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.discreteseekbar; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 widget. 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.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.discreteseekbar" xmlns:android="http://schemas.android.com/apk/res/android"> <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 above.
That’s end of tutorial on Android Discrete SeekBar Widget.
You must be logged in to post a comment.