How do you rate this blog

Wednesday, January 4, 2012

SeekBar

A seekbar is just like a progress bar with a dragger in it which the user can set. In this post I have just created a simple seekbar which shows the value in a text below and shows if the tracking is on if the touch is present over the tracker.
First create a new project. Then open file main.xml under resà layouts. Remove the default text there and add a text which will show if the tracking is on or not. Below that add the seekbar control. Below this add one more text which will show the value to which the slider has been dragged to. The layout will look as shown in the snapshot below.


Then we need to bind the functionality with the UI controls so open the java file under src à your package. You can see already there will be basic code is already there. The class will by default extend Activity you need to implement OnSeekBarChangeListener, once you do this you will need to override other functions onProgressChanged, onStartTrackingTouch, onStopTrackingTouch. Then declare the seekbar and the 2 text views which will show the tracking status and the value.
Then create a seekbar and create a listener for that as shown below-
sb=(SeekBar) findViewById(R.id.seekBar1);
sb.setOnSeekBarChangeListener(this);
Then you need to declare and bind the textboxes. Once this is done you need to add functionality when the seekbar position is changed, when the tracking has started, when the tracking has stopped as shown below.
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean TouchStart) {
                                sv.setText("Progress="+String.valueOf(progress));
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
                                title.setText("Tracking On");
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
                                title.setText("Tracking Off");
}
 Once this is done we are done and the file will look as in the snapshot given below.

Now run the project in your emulator or mobile. Drag the slider and the text above the seekbar should change to Tracking On and when u stop it should say Tracking off and the value in the textbox below the seekbar should show value at which the slider has been dragged to as show in the snapshot given below.


No comments:

Post a Comment