Java code for an Android SeekBar

Android Widgets: 1 2 3 4 5 6 7 8 9

In the previous lesson, you added a SeekBar to your layout. In this lesson, we'll write the code for it.

Open up your MainActivity.java file. In the onCreate method, add the following line, just below setContentView:

final SeekBar mySeekBar = (SeekBar) findViewById(R.id.seekBar);

Here, we're getting a reference to a SeekBar with the ID seekBar. This is the one on the layout.

As usual, if you get any red text then press ALT + ENTER to import the correct library. Or type import android.widget.SeekBar; at the top of your code:

The onCreate method getting a reference to a SeekBar

SeekBars can have a listener attached to them. The one we'll use is called setOnSeekBarChangeListener. This listens out for changes to the SeekBar's slider. Add the following code just before the final curly bracket of the onCreate method:

mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

});

Pay special attention to where all the round and curly brackets are. But this listener accepts a new OnSeekBarChangeListener object between its round brackets. You then a pair of curly brackets followed by a final round bracket and then a semicolon. Your code window should look like this:

Adding a Listener to the onCreate method

Note the red underline in the code. This is because this particular listener needs you to override three methods. The three methods are onProgressChanged, onStartTrackingTouch (you rarely need any code for this method), and onStopTrackingTouch.

Add the following three overrides to your code. Add them just before the }); at the end of the listener:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

And here's what your onCreate method should look like now:

Adding three override methods to the listener

Notice that the red underline we had before has now gone. If you're still getting red underlines, make sure all your round and curly brackets are in the right place.

In the onProgressChanged method, you can make use of the int progress variable. This tells you what number the slider has reached. So if you set the slider to 7, for example, the progress variable will store this 7 for you.

Add this int variable just above onProgressChanged:

int seekBarProgress = 0;

Adding a variable to track the SeekBar progress

Now, in between the curly brackets of onProgressChanged, we can store the progress into our seekBarProgress variable:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

seekBarProgress = progress;

}

When you lift your finger of the SeekBar slider, the onStopTrackingTouch method will be activated. We can write code here to display a message (or adjust the brightness/volume if you were doing that). We'll use our seekBarProgress variable to work out where the slider is.

Add this method underneath your onCreate method:

public void toastMessage( int sliderValue ) {

}

The name toastMessage is just something we came up with. In between the round brackets of this new method we're setting up an int variable called sliderValue.

Now go back to the onStopTrackingTouch method and make a call from your new toastMessage method from there:

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

toastMessage(seekBarProgress);

}

Your code should look like this, so far:

Adding a Toast method to the code

 

Android Toasts

In Android, a toast is the name given to a message box that appears on screen for only a brief time. They have this format:

Toast.makeText( context, MESSAGE_TO_DISPLAY, Toast.LENGTH_VALUE).show();

So the method to call on the Toast object is called makeText. In between the round brackets of makeText you first need a context, such as MainActivity.this, meaning this particular layout in the MainActivity class. After a comma, you type your message, either in direct text surrounded with quotation marks, or the name of a String variable. Next, you need the length your Toast will display for. This is either LENGTH_LONG or LENGTH_SHORT. After the round brackets of makeText, you type a dot then call the show() method.

Let's add some logic to our method. The logic will test what is inside of the sliderValue variable we passed to our method.

Add the following code between the curly brackets of your toastMessage method (you can change the messages, though, to anything you like):

String message = "";

if (sliderValue < 5) {

message = "Either you're really ugly or your confidence is shot to pieces!";

}

if (sliderValue >= 5 && sliderValue <= 8 ) {

message = "Good score - I guess you're not hiding in the attic!";

}

if (sliderValue == 9 || sliderValue == 10 ) {

message = "Wow! Either you're incredibly beautiful or incredibly vain";

}

The IF Statements just test for three sets of values: less than 5; greater than or equal to 5 and less than or equal to 8; the values 9 or 10.

Finally, add the Toast:

Toast.makeText( MainActivity.this, message, Toast.LENGTH_LONG ).show();

(You may get red text for Toast. So press ALT + ENTER to add the library. Or type import android.widget.Toast; at the top of your code.)

The whole of you code should look like this:

The whole of our Java code for a SeekBar

Try it out, now. Run your app and move the slider along. You will see something like this:

An app showing the use of a SeekBar

 

OK, we'll move on from sliders and tackle the CheckedTextView. We'll do that in the next section of the course.