33

I'm trying to get the value of the seek bar whenever it changes and display it underneath. I'm using the onclick method on my seekbar to call this method.

public void getNumber(View view) 
{
    SeekBar seek = (SeekBar) findViewById(R.id.seekBar1);
    int seekValue = seek.getProgress();
    String x = "Value: " + Integer.toString(seekValue);
    ((TextView) findViewById(R.id.level)).setText(x);
}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
user2154475
  • 386
  • 1
  • 6
  • 10

1 Answers1

52

Here some extra help:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.SeekBar; 
import android.widget.TextView; 

public class AndroidSeekBar extends Activity { 
   /** Called when the activity is first created. */ 
   @Override 
   public void onCreate(Bundle savedInstanceState) 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 

       SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar); 
       final TextView seekBarValue = (TextView)findViewById(R.id.seekbarvalue); 

       seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ 

   @Override 
   public void onProgressChanged(SeekBar seekBar, int progress, 
     boolean fromUser) { 
    // TODO Auto-generated method stub 
    seekBarValue.setText(String.valueOf(progress)); 
   } 

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

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

use the OnProgressChanged event to check the progress of the seekbar, you can return that int to use it somewhere else in your app.

Max
  • 12,622
  • 16
  • 73
  • 101
  • where do you put this code? Create a new .java file and plop it in? – user2154475 Mar 10 '13 at 19:39
  • You could do that, but there should already be a file in your current project where you can put your code. – Max Mar 10 '13 at 20:48
  • What if I want to get the progress before it is changed, let's say during on create? – Si8 Nov 15 '16 at 12:11
  • @Si8 you should be able to access it from the onCreate method. I think calling getProgress() on seekBar should do the trick. – Max Nov 15 '16 at 12:12