1

The below code is not working when I click on RatingBar in App. Please Help...

Note: I have set the property of RatingBar Onclick with rate.

public void rate(View v) {
    RatingBar r1 = (RatingBar)findViewById(R.id.ratingBar);
    Toast.makeText(MainActivity.this, String.valueOf(r1.getRating()) ,Toast.LENGTH_SHORT).show();

}

Rating Bar and Properties Panel

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
Ramakant
  • 185
  • 1
  • 3
  • 10

2 Answers2

2

It seems you would like to be informed whenever users set the rating, so you would need OnRatingBarChangeListener not OnClickListener.

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

    }
});

Note: onClick method of OnClickListener interface is getting called by another method called performClick(). This method is declared in the View class, so click event all is handled in View class. However RatingBar overrides internal touch event callbacks and does not allow the root class (i.e. View) to be informed. So, the underlying View cannot detect click events anymore. A workaround nevertheless, could be in this post.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
0

The reason for Onclick property is not working is that RatingBar overrides onTouchEvent() . so ratingBar.setOnClickListener() is never called.

use OnTouchListener instead of onClickListener.

sivaBE35
  • 1,876
  • 18
  • 23