Large click-through index

I would like to register an event that you can click on the thumb to trigger an event when the user has set it. Is it possible?

+7
source share
4 answers

Combining zwebie and Nermeens answers the right solution:

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { private int mProgressAtStartTracking; private final int SENSITIVITY; @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { // handle progress change } @Override public void onStartTrackingTouch(SeekBar seekBar) { mProgressAtStartTracking = seekBar.getProgress(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { if(Math.abs(mProgressAtStartTracking - seekBar.getProgress()) <= SENSITIVITY){ // react to thumb click } } }); 

Thus, this really only works when you press the thumb, and not when you move the thumb, and not when you click on other parts of the arrow.

You can adjust the sensitivity, because sometimes a click already moves the thumb a little and gives small changes, and clicking becomes less frustrating. A good value here depends on the size of the arrow and the maximum value that it can have. For me, 3 worked well on a full-sized search bar with 50 max on the portrait layout.

+2
source

I was able to achieve this as follows:

 seekBar.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_MOVE) { changedPosition = true; seekBar.setProgress(seekBar.getProgress()); return false; } else if (event.getAction() == MotionEvent.ACTION_UP) { if(!changedPosition) { //do action here } } } 

hope this helps

+1
source

I used Bij's answer as inspiration. This works when the thumb moves so if the user does not click on the thumb, he will not move. Hope this helps. :)

If you have any questions about how this works, feel free to comment.

  mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progress = mSeekBarSpeed.getProgress(); boolean started = false; //use this variable to see whether the user clicked the right place @Override public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) { if(!started){ //check to see if user clicks the right place //if the user clicks within a specific threshold float threshold = (float)seekBar.getMax() / seekBar.getWidth() * seekBar.getThumb().getIntrinsicWidth() / 2; if(Math.abs(progressValue - progress) < threshold){ if(fromUser){ //checks if user actually clicked it started = true; } }else{ seekBar.setProgress(progress); //set to original progress onStopTrackingTouch(seekBar); //not really necessary, keep or delete based on your needs return; //get out of method } } if(started) { progress = progressValue; //update progress variable System.out.println("onProgressChanged:" + progress + "/" + seekBar.getMax()); //DO WHAT YOU NEED TO DO WHEN PROGRESS IS CHANGING } } @Override public void onStartTrackingTouch(SeekBar seekBar) { System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax()); } @Override public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax()); //DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING started = false; //remember to set variable to false } }); 
+1
source

I do this:

 pagesSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //listener @Override public void onStopTrackingTouch(final SeekBar seekBar) { //add your event here } @Override public void onStartTrackingTouch(final SeekBar seekBar) { } @Override public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { updateControls(progress, false); } }); 
0
source

All Articles