Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture SeekBar values

In my android application I have a TextView and a SeekBar. Both the values are used for calculating result on a button click.

The problem is how to capture the SeekBar value and convert it into String for the calculation.

The code is below:

class clicker implements Button.OnClickListener {
    public void onClick(View v) {
        String a,b;
        Integer vis;
        a = txtbox3.getText().toString();
        b = slider1.getContext().toString();
        vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
        tv.setText(vis.toString());
    }
}

Please Help. Thanks in advance.

like image 981
Abhishek Avatar asked Jan 25 '11 08:01

Abhishek


2 Answers

You are looking for the method getProgress() of the ProgressBar class as SeekBar is a subclass of ProgressBar.

So basically it would be something like that.

int value = seekBar.getProgress();

Also I don't really understand why you want to convert an int to a String just so you can convert that String to an Integer later. It does not make any sense.

like image 100
Octavian A. Damiean Avatar answered Oct 14 '22 09:10

Octavian A. Damiean


You can store the progress value into a global integer variable. Then you can use it easily where ever you want. Try this Code. It will help you...

int p=0;
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

final TextView textView=(TextView) findViewById(R.id.t1);
    final SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
final Button button=(Button) findViewById(R.id.button1);

    seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
        // TODO Auto-generated method stub
        p=progress;
    }
});


 button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String a = textView.getText().toString();
        vis = (Integer.parseInt(a)*p)/100;
            text.setText(vis.toString());
    }
});
like image 23
Balaji Gunasekar Avatar answered Oct 14 '22 09:10

Balaji Gunasekar