Search code examples
javaandroidandroid-layoutandroid-viewratingbar

MaterialRangeBar: setOnRangeBarChangeListener method


I'm trying to implement an option in android app where users can use range-bar to set the radius of where they want the application to seek for nearby friends. For the range bar I'm using this library GitHub page. Here is also the part of the .xml code for my range-bar from my activity.

<com.appyvet.materialrangebar.RangeBar
        android:layout_marginEnd="4dp"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:id="@+id/radiusRangeBar"
        app:mrb_rangeBar="false"
        app:mrb_tickStart="1"
        app:mrb_tickEnd="5"
        app:mrb_tickInterval="1"
        app:mrb_pinRadius="8dp"
        app:mrb_pinMaxFont="8sp"
        app:mrb_barWeight="1dp"
        app:mrb_tickColor="@color/colorPrimary"
        app:mrb_connectingLineColor="@color/colorPrimaryDark"
        app:mrb_connectingLineWeight="3dp"
        app:mrb_pinColor="@color/switchColor"
        app:mrb_selectorColor="@color/switchColor"
        app:mrb_selectorSize="10dp"/>

What I want to do?

I want when the user try to move the selector to receive message. This is the function responsive for that.

RangeBar radiusRangeBar;
AlertDialog alertDialog;
AlertDialog.Builder builder;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_settings, container, false);
radiusRangeBar = (RangeBar) view.findViewById(R.id.radiusRangeBar);

radiusRangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
                builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("You are about to set the Nearby friends radius on " + rightPinValue + "km") ;
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(true);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(false);
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
            }
        });
}

What is wrong? - Since this was the only function mentioned in the documentation that from the GitHub page I tried to implement it but It's not appropriate for my setup because it displays message as I'm scrolling and I want that to happen only when I'll stop scrolling. Is that doable or I have to try another method to notify the user for his action.


Solution

  • I addition to the answer from azizbekian. I have updated my code and posted here so every person in future to be able to solve his problem quick and easy.

    This is the setOnRangeBarChangeListener now.

     radiusRangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
                rangeBarValue = rightPinValue;
                // first remove the previous action if it exists
                handler.removeCallbacks(showDialogRunnable);
                // schedule a new action to take place in 700ms
                handler.postDelayed(showDialogRunnable, 700);
            }
        });
    

    I've created string variable which initialized by moving the rangebar selector.

    Then if no action is going on the handler.postDelayed(showDialogRunnable, 700); will trigger;

    Here is the code for this function as well.

    private final Handler handler = new Handler();
    final Runnable showDialogRunnable = new Runnable() {
                @Override
                public void run() {
                    builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("You are about to set the Nearby friends radius on " + rangeBarValue + "km") ;
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            privateAccountsSwitch.setChecked(true);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            privateAccountsSwitch.setChecked(false);
                        }
                    });
                    alertDialog = builder.create();
                    alertDialog.show();
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
                }
            };