0

I am having some problem when trying to highlight the ratingBar star based on the data returned from database. So basically I have this ratingBar:

ratingBar = (RatingBar) eventChat.findViewById(R.id.ratingBar);

And then I perform a check for the rates return from database, if not null then I wanted to set the highlighted star based on the integer:

if (reviewModel.getEventReviewRate() != null) {
        existStarRate = Integer.parseInt(reviewModel.getEventReviewRate());
        Log.i("existStarRate", String.valueOf(existStarRate));
    }
    if (existStarRate <= 0) {
        LayerDrawable stars = (LayerDrawable) ratingBar
                .getProgressDrawable();
        stars.getDrawable(2).setColorFilter(
                (context.getResources().getColor(R.color.lightred)),
                PorterDuff.Mode.SRC_ATOP);
        ratingBar.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                return true;
            }
        });
    } 

I customize the highlighted star by these codes:

LayerDrawable stars = (LayerDrawable) ratingBar
                .getProgressDrawable();
        stars.getDrawable(2).setColorFilter(
                (context.getResources().getColor(R.color.lightred)),
                PorterDuff.Mode.SRC_ATOP);

However, I have no idea how to set the highlighted star dynamically. Let's say currently the data returned from database is 4, I should set 4 of my ratingBar star to be highlighted in lightred color.

Any ideas?

Thanks in advance.

2 Answers2

0

Use Greater than instead of less than

    if (existStarRate **>**= 0) {
    LayerDrawable stars = (LayerDrawable) ratingBar
            .getProgressDrawable();
    stars.getDrawable(2).setColorFilter(
            (context.getResources().getColor(R.color.lightred)),
            PorterDuff.Mode.SRC_ATOP);
    ratingBar.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            return true;
        }
    });
MathanG
  • 1,195
  • 10
  • 22
  • What I am trying to do is if less than 0 which means no record in my database, I allow them to rate. But now I need to highlight the star based on the data returned from database which is in my if statement –  Nov 17 '14 at 09:15
  • I think u need to set different style or different selectors for rating bar depends on the rating from the database. refer [link](http://stackoverflow.com/questions/5800657/how-to-create-custom-ratings-bar-in-android) to create custom rating bar. create 5 different styles or selectors and apply them depends on rating from database. – MathanG Nov 17 '14 at 09:24
  • Yeah by highlighting it. Let say currently I have 5 stars and the data returned from database was 4. So I need 4 of them to be highlighted in lighred color –  Nov 17 '14 at 09:27
  • you want first 4 stars to be in one style and last one star is in other style is that so? – MathanG Nov 17 '14 at 09:34
  • Yeah, the first 4 should be in lightred while the last one will be default color –  Nov 17 '14 at 09:37
  • That is not possible coz rating bar is a single view. but you can achieve it by 5 image views aligned horizontally and assign lightred star image to first 4 of them and set default star to last one. Then set on click listener to all of the imageview and change upto clicked image to show current rating.(this solution works if you are using only integer ratings) – MathanG Nov 17 '14 at 09:41
  • But then there are some example like google playstore, they also using ratingBar with highlighted star with data fetched from database. Any ideas? –  Nov 17 '14 at 09:44
  • sorry i don have any idea about that – MathanG Nov 17 '14 at 09:46
  • I found out the solution already. You could simply use .setRating(int); –  Nov 17 '14 at 10:05
0

I've figured out the solution already. You could simply use .setRating(int) and it should be highlighted in default blue color