Tuesday, March 16, 2021

Android SeekBar

Android SeekBar Example

android seekbar

Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.

The SeekBar.OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.

Android SeekBar and RatingBar classes are the sub classes of AbsSeekBar.


Android SeekBar Example

activity_main.xml

Drag the seek bar from the pallete, now activity_main.xml will look like this:

File: activity_main.xml


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.seekbar.MainActivity">  
  8.   
  9.   
  10.     <SeekBar  
  11.         android:id="@+id/seekBar"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginEnd="8dp"  
  15.         android:layout_marginStart="8dp"  
  16.         android:layout_marginTop="372dp"  
  17.         app:layout_constraintEnd_toEndOf="parent"  
  18.         app:layout_constraintStart_toStartOf="parent"  
  19.         app:layout_constraintTop_toTopOf="parent" />  
  20. </android.support.constraint.ConstraintLayout>  


Activity class

Let's see the Activity class displaying seek bar and performing event handling.

File: MainActivity.java
  1. package example.javatpoint.com.seekbar;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.SeekBar;  
  6. import android.widget.Toast;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.     SeekBar seekBar;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.   
  15.         seekBar=(SeekBar)findViewById(R.id.seekBar);  
  16.         seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
  17.             @Override  
  18.             public void onProgressChanged(SeekBar seekBar, int progress,  
  19.                                           boolean fromUser) {  
  20.                 Toast.makeText(getApplicationContext(),"seekbar progress: "+progress, Toast.LENGTH_SHORT).show();  
  21.             }  
  22.   
  23.             @Override  
  24.             public void onStartTrackingTouch(SeekBar seekBar) {  
  25.                 Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();  
  26.             }  
  27.   
  28.             @Override  
  29.             public void onStopTrackingTouch(SeekBar seekBar) {  
  30.                 Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
  31.             }  
  32.         });  
  33.     }  
  34. }  

Output:

android seekbar example 1 android seekbar example 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile