简体   繁体   中英

How to check if a rating bar is has been rated or not in android?

I have a rating bar in my app and I want to check if the user has rated on the rating bar or not. If it been has rated by the user then , an intent takes you to the next screen and if he has not rated then there is toast message which says "Please rate us!!"

First, you have to set android:rating="0.0" to your RatingBar in xml. Then on button click check,

RatingBar ratingbar = (RatingBar)findViewById(R.id.ratingBar);
@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ratingBar:
                 if(ratingbar.getRating() == 0.0){

                    //set what you want
                     }else{

                    //give error if ratingbar not changed
                     }
            break;
    }
}

Implement the onRatingBarChangeListener and place the intent for the new activity inside it:

    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {

            // place intent for new activity

        }
    });

Use the RatingBar.getRating() method to get the current rating. If it returns zero, then prompt the user to rate your app with a toast.

You can more about Rating Bar and its methods here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM