0

I am having some logic problem with my Java code to prompt dialogue box. So basically when my ratingBar is onTouch, I will get the star rate and prompt dialogue box:

ratingBar.setOnTouchListener(new OnTouchListener()
    { 
        public boolean onTouch(View view, MotionEvent event)
        { 
            float touchPositionX = event.getX();
            float width = ratingBar.getWidth();
            float starsf = (touchPositionX / width) * 5.0f;
            starRate = (int)starsf + 1;
            ratingBar.setRating(starRate);
            promptSubmitStar();
            return true; 
        } 
    });

 public void promptSubmitStar(){
    AlertDialog.Builder Dialog = new AlertDialog.Builder(getActivity());
    Dialog.setTitle("Confirm Rating");
    LayoutInflater li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.option_submit_star, null);
    txtPromptStarRate = (TextView) dialogView.findViewById(R.id.txtPromptStarRate);
    txtPromptStarRate.setText("Confirm to submit " + starRate + " stars for this event?");
    Dialog.setView(dialogView);
    Dialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {          
                    dialog.dismiss();
                }
            });

    Dialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {
                    dialog.dismiss();
                }
            });
    Dialog.show();
}

But with these codes, when user selected the star rate, the dialogue box prompted twice. I wonder which part is causing it. Thanks in advance.

1 Answers1

0

It's because onTouch listener is called multiple times, i.e. when your finger lands on the screen (ACTION_DOWN) and when you move your finger up (ACTION_UP).

You want to show the dialog when the latter one fires:

ratingBar.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float touchPositionX = event.getX();
            float width = ratingBar.getWidth();
            float starsf = (touchPositionX / width) * 5.0f;
            starRate = (int) starsf + 1;
            ratingBar.setRating(starRate);
            promptSubmitStar();
        }
        return true;
    }
});

I'm not sure why you'd use a touch listener though. You can listen to the RatingBar changes like this:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar bar, float rating, boolean user) {
        promptSubmitStar();
    }
});
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Because my rating bar is very small and through my researches, I know I could use the touch listener :) Thanks a lot, it's solved now! –  Nov 16 '14 at 13:57
  • By the way, do you have any idea how to disable the rating bar after user selected okay? Because I tried with ratingBar.setIndicator(true) and it does not work –  Nov 16 '14 at 14:03
  • @IWasSoLost first thing that comes to mind is to set a new touch listener, that just always returns true. – Simas Nov 16 '14 at 14:05
  • DO you have any ideas how to set highlighted star based on the data fetch from database? http://stackoverflow.com/questions/26968863/android-ratingbar-fetch-data-dynamically –  Nov 17 '14 at 09:06