0

I want to attach along click action to seekbar to do some thing.

And I tried to do this by implementing setOnLongClickListener on onStopTrackingTouch and onProgressChanged methods and it is not working.

   seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, final int progress, boolean fromUser) {
            textView.setText("" + progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            seekBar.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    ToastMaker("LongPress");
                    return false;
                }
            });
        }
    });

my final goal is to set seekbar's steps 10 or 20 for onProgressChanged and when user holds seekbar the step value changes to 1 (using incrementProgressBy(1) method) so user can be able to select exact value on a long range seekbar.

if you use Samsung's Video editor they did same thing. you can scroll through video timeline fast and when you want to select the exact moment you hold the seekbar and you can select exact moment you like(look at the gif bellow)

i.stack.imgur.com/HxNGc.gif

Morteza
  • 9
  • 6

1 Answers1

1

You could try to override the touch events of the seekbar with onTouchEvent() like this

private var isLongPress = false
private val longPressHandler = Handler()
private val onLongPress = Runnable {
isLongPress = true

    //do whatever you want on long press
}
private val onPress = {
    //do on normal press
}

override fun onTouchEvent(event: MotionEvent): Boolean {
    when (event.action) {
        MotionEvent.ACTION_DOWN -> {
            //on action down we start the long press handler
            longPressHandler.postDelayed(onLongPress, 500)
        }
        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
           if (isLongPress) {
                //if 500ms passed and user lifts the finger
                isLongPress = false
            } else {
                //if 500ms didn't pass and user lifts his finger before that time we cancel long press click and treat it as normal press
            longPressHandler.removeCallbacks(onLongPress)
            onPress()
            }
        }
    }
    return true
}
laszlo
  • 494
  • 4
  • 18
  • You can do this automatically, please refer to this answer https://stackoverflow.com/questions/34957430/how-to-convert-a-kotlin-source-file-to-a-java-source-file – laszlo Aug 12 '19 at 12:47