oli107 / material-range-bar

Android widget for selecting a range of values.
Apache License 2.0
1.67k stars 370 forks source link

How to prevent overlapping two sliders #172

Open swatiomar opened 5 years ago

swatiomar commented 5 years ago

I want to prevent the overlapping issue. As Left slider don't pass the right slider or not overlap the right slider. How to do this?

Mukarrim commented 5 years ago

I solved it by implementing this little solution, hope it helps.

    int preMin = -1;
    int preMax = -1;

rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(final RangeBar rangeBar, int leftPinIndex,
                                              int rightPinIndex, String leftPinValue, String rightPinValue) {
                int diff = rightPinIndex - leftPinIndex;
                if (diff < 3) {
                    rangebar.setEnabled(false);
                    if (leftPinIndex != preMin || rightPinIndex != preMax) {
                        rangeBar.setRangePinsByIndices(preMin, preMax);
                    }
                    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                    alert.setNegativeButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            rangeBar.setEnabled(true);
                            dialog.dismiss();
                        }
                    });
                    alert.setCancelable(true);
                    alert.setMessage(Html.fromHtml("Minimum range reached!!")).show();

                } else {
                    preMin = leftPinIndex;
                    preMax = rightPinIndex;
                }

                // More of what you desire
            }

            @Override
            public void onTouchEnded(RangeBar rangeBar) {
                Log.d("RangeBar", "Touch ended");
            }

            @Override
            public void onTouchStarted(RangeBar rangeBar) {
                Log.d("RangeBar", "Touch started");
            }
        });
ghost commented 4 years ago

Any updates here? I also think having an option of overlap is great!

akshay-nak commented 4 years ago

Setting minThumbDistance=1 did it for me.

ramazanogunc commented 2 years ago

Thanks for advice @akshay-nak . It worked for me :)