Hello Readers! In this post, we will learn about how to use android seekBar widget. We will also go through different attributes of seekBar that can be used to customise this widget.
Output
Getting Started
Android SeekBar can be defined as below –
SeekBar is an extension of progressBar that has a draggable thumb. User can drag this thumb back and forth to set current progress. Placing focusable widgets to left and right of the seekBar is discouraged.
Attributes of Android SeekBar Widget
Some of the popular attributes of android seekBar that can be used to customise this widget are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:thumb | It draws a thumb on seekbar. |
Some of the popular attributes of android SeekBar inherited from AbsSeekBar are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:thumbTint | Tint to apply on drawable of thumb. |
2 | android:thumbTintMode | Blending mode used to apply the thumb tint. |
3 | android:tickMarkTint | Tint to apply on tick mark drawable. |
4 | android:tickMarkTintMode | Blending mode used to apply the tick mark tint. |
Some of the popular attributes of android SeekBar inherited from ProgressBar are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:max | It defines the maximum value |
2 | android:min | It defines the minimum value |
3 | android:progress | It defines the default progress value (between 0 and max value) |
4 | android:progressDrawable | It defines drawable of the progress mode. |
Some of the popular attributes of android SeekBar inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:background | Sets background of the view |
2 | android:clickable | Set whether view is clickable or not. |
3 | android:elevation | Sets base z-depth of the view |
4 | android:id | Sets id of the view. |
Example of Android SeekBar Widget
At first, we will create android application. Then, we will use seekBar widget in this application.
1. Creating New Project
Follow the steps below to create new project. Please ignore the steps if you have already created an application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as SeekBar. 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 widget in the project.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">SeekBar</string> </resources>
3. Use 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" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"/> </LinearLayout>
In activity_main.xml file, we have used seekBar widget. Now, we will access this seekBar into java file. Also, we will show a toast message that displays current progress of the seekBar.
4. Access SeekBar Widget in java file
Open src/main/java/com.tutorialwing.seekbar/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.seekbar; 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, "Progress is " + seekBar.getProgress() + "%", Toast.LENGTH_SHORT).show(); } }); } } }
In MainActivity.java, We have accessed SeekBar widget. Also, we are showing a toast message whenever thumb is moved back and forth that displays current progress of the seekBar.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.seekbar" 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 SeekBar Widget.
You must be logged in to post a comment.