florent37 / SingleDateAndTimePicker

You can now select a date and a time with only one widget !
Apache License 2.0
1.02k stars 333 forks source link

How can I apply vibration while spinning the SingleDateAndTimePicker wheel, like iOS? #287

Open ShawnMuktadir opened 4 years ago

Muhammadsher commented 3 years ago

Not the perfect one but it works somehow

dateAndTimePicker.addOnDateChangedListener((displayed, date) -> {
            Vibrator vibrator = (Vibrator) Objects.requireNonNull(getContext()).getSystemService(Context.VIBRATOR_SERVICE);
            final long[] pattern = {0, 10};
            final int[] amplitudes = {50, 50};

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                VibrationEffect effect = VibrationEffect.createWaveform(pattern, amplitudes, 0);
                assert vibrator != null;
                vibrator.vibrate(effect);
                (new Handler()).postDelayed(vibrator::cancel, 10);
            } else {
                assert vibrator != null;
                vibrator.vibrate(10);
            }

        });
ShawnMuktadir commented 3 years ago

works great! but, if I set delayMillis to 50, then its works perfectly, like (new Handler()).postDelayed(vibrator::cancel, 50);