mihirjoshi
mihirjoshi

Reputation: 12201

Prevent certain rating on RatingBar

Rating bar has a OnRatingBarChangeListener which worked only when the rating is already been changed by the user. I want when the user click on the rating bar to put a 3 star rating, RatingBar prompts a Toast that 3 star rating is not allowed.

Upvotes: 1

Views: 270

Answers (2)

vishnus
vishnus

Reputation: 728

U can get the rating set by the user through the onRatingBarChangedListener in the onRatingChanged method.

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

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

        if(rating == 3){
             //show toast here. then may be reset rating to 0.
        }

     }
});

Upvotes: 1

Ashwani Kumar
Ashwani Kumar

Reputation: 1472

this code will help you.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rating);
        RatingBar ratingBar;
        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
                // TODO Auto-generated method stub
                if(rating == 3){
                    Toast.makeText(getApplicationContext(), "3 star rating is not allowed.", Toast.LENGTH_LONG).show();
                    ratingBar.setRating(0);
                }
            }
        });
    }

Upvotes: 2

Related Questions