Friday, March 23, 2012

How to get the current progress on your SeekBar in android?


How to get the current progress on your SeekBar in android?

I have already shown how to use seekBar in android.
This simple snippet helps you to get the current progress in the seekbar.
It’s same code from this 
post, only thing is I added the getProgress method.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.coderzheaven;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
 
public class SeekBarDemo extends Activity  implements SeekBar.OnSeekBarChangeListener {
    SeekBar mSeekBar;
    TextView mProgressText;
    TextView mTrackingText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
        mSeekBar = (SeekBar)findViewById(R.id.seek);
        int p = mSeekBar.getProgress();
        System.out.println("Current Progress = " + p);
        mSeekBar.setOnSeekBarChangeListener(this);
        mProgressText = (TextView)findViewById(R.id.progress);
        mTrackingText = (TextView)findViewById(R.id.tracking);
    }
 
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        mProgressText.setText(progress + " " +
                getString(R.string.seekbar_from_touch) + "=" + fromTouch);
 
        System.out.println("onProgressChanged >> Current Progress = " + progress);
    }
 
    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_on));
    }
 
    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_off));
    }
}
Check the Logcat for the current progress or the TextView in which I am showing.
Take the xml from the This post
Please leave your valuable comments.

No comments: