Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android SeekBar Tutorial

Android SeekBar Tutorial

Hello Developers, In this tutorial, we will learn about Android Seekbar. Android SeekBar widget is an extension of Android ProgressBar, however it is used as an input widget rather than showing the progress to the user.

User set the current progress by touching the thumb and drag left or right arrow keys. Android Seekbar has three distinct states:

  1. drag start
  2. changing
  3. drag ended

Android SeekBar is used in brightness control, volume control or moving to a particular location in a video or music app like YouTube as shown below.


Create Android Seekbar App

  1. Go to File → New → New Project and enter your Application Name. Let’s say Android SeekBar Example
  2. Enter company domain, this is used to uniquely identify your App’s package worldwide.
  3. Choose project location and minimum SDK and on the next screen choose Simple Activity, since we would be adding most of the code Ourselves. Then Click on Next.
  4. Choose an Activity Name. Make sure Generate Layout File check box is selected, Otherwise we have to generate it ourselves.Then click on Finish. We have kept the Activity Name as MainActivity.java. This will be the default screen when the user opens the app for the first time.
  5. Gradle will configure your project and resolve the dependencies, Once it is complete proceed to the next steps.

    To add a SeekBar to a layout (XML) file, you can use the element.

    Add Layout for Android SeekBar Example

    1. Add the following code to the activity_main.xml. The layout consist of an Android SeekBar and two TextView.
    2. activity_main.xml

      Since android SeekBar is an extension of ProgressBar most of the Attribute are same.

    Implementing Android Seekbar Listener

    Open MainActivity.java and add the following code.

    MainActivity.java

    package com.androidtutorialpoint.androidseekbarexample;
    
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.NumberPicker;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.Toast;
    import org.w3c.dom.Text;
    
    public class MainActivity extends AppCompatActivity {
        private SeekBar seekbar;
        private TextView progressTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            seekbar = (SeekBar) findViewById(R.id.seekbar);
            progressTextView = (TextView) findViewById(R.id.progress);
            seekbar.setMax(150);
            seekbar.setProgress(20);
    
            // perform seek bar change listener event used for getting the progress value
            seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                int progressChangedValue = 0;
    
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    progressChangedValue = progress;
                }
    
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }
    
                public void onStopTrackingTouch(SeekBar seekBar) {
                    progressTextView.setText("Your current progress is "+progressChangedValue);
                    Toast.makeText(MainActivity.this, "Seek bar progress is :" + progressChangedValue,
                            Toast.LENGTH_SHORT).show();
                }
            });
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    To notify the client that the progress level has been updated on the android seekbar, we need to implement OnSeekBarChangeListener.

    seekBarInstanceVariable.setOnSeekBarChangeListener(new OnSeekBarChangeListener() – This method is used to notify the user changes/actions in the SeekBar.

    We need to implement these three methods.

    1. onProgressChanged (SeekBar seekBar, int progresValue, boolean fromUser) – This is a notification that the progress level has changed on the android seekBar. The parameter fromUser distinguishes user-initiated changes from the programmatic changes.
    2. onStartTrackingTouch(SeekBar seekBar) – This is a notification that the user has started a touch gesture i.e the user has started to drag the thumb. We may use this method if want to lock the seekbar at the current level and prevent advancing.
    3. onStopTrackingTouch(SeekBar seekBar) –This is a notification that the user has finished the touch gesture i.e the user has stopped dragging the thumb. We may use this method if want to activate the seekbar at the current level and resume advancing.

    Apart from these we can use getMax() to get the max value of the android SeekBar in the form of integer.

    To get the progress, We can get the current progress value from a Seekbar using getProgress() method. This method returns an integer value.

    Attributes of a SeekBar

    1. max – used to define the maximum progress value for the Android SeekBar. Instead of setting the max value in the xml, you can use the setMax() method to set the maximum progress value programmatically.
    2. progress – used to define the default progress value, should be an integer between 0 and max. Alternatively you can use the setProgress() method to set the progress value programmatically for the android seekbar.
    3. progressDrawable – We can provide a custom drawable xml for the progress mode of a seekbar. Progress drawable is used when we need to display a custom progress for the android seekbar as shown in figure below. We have used a custom shape
    4. thumb – It is used to set a custom thumb for the android seekbar example, as shown in the figure below for the brightness we have custom thumb like sun
    5. indeterminate – This attribute is used for enabling the indeterminate mode for Android SeekBar. As the name suggests the actual progress will be indeterminate and user will see a cyclic animation
    6. rotation – This attribute can be used to change the orientation of the Android SeekBar as shown in image below.

    Now run the Android SeekBar Example app on an emulator or an actual Android Device and you should be able to see the progress on the Android SeekBar. With the knowledge of Android SeekBar you can use it to take range input from the user like the brightness or volume control.

    Custom Android SeekBar Implementations

    You can experiment with the following custom implementations for use in your app provided by respective developers.

    1. Android PreviewSeekBar
    2. Android SeekBar Compat
    3. Discrete SeekBar

    What’s Next ??

    After this, We will be covering more interesting tutorials like creating a multilingual app in android.
    Till then stay tuned for more tutorials.. and Don’t forget to subscribe our blog for latest android tutorials. Also do Like our Facebook Page or Add us on Twitter.

    To download the full code for the Android Image Slider Tutorial using the android studio, Click on the Download Now link below.


    The post Android SeekBar Tutorial appeared first on Android Tutorial Point.



This post first appeared on Android Tutorial Point, please read the originial post: here

Share the post

Android SeekBar Tutorial

×

Subscribe to Android Tutorial Point

Get updates delivered right to your inbox!

Thank you for your subscription

×