1

I have my android rating bar with the following code:

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:stepSize="1.0" />

I want to initialize the first star value to -5, so that the remaining stars will get the values like -4,-3,-2...
But I don't know how to give that initial value to 1st star of my rating bar in android.
And I want my rating bar with three colors:

  • for the values -5,-4,-3,-2,-1 the star color should be RED,
  • for the value 0 star color should be BLUE
  • and for the values 1,2,3,4,5 the star color should be GREEN.
GitaarLAB
  • 13,494
  • 9
  • 51
  • 74
honeyprince
  • 29
  • 3
  • 7

2 Answers2

2

The minimum rating can be 0, negative numbers are not allowed.

However, you can create a rating bar with 11 stars to represent the values (-5 to +5)

In the listener of the rating bar, map the value to the range of -5 to +5 (by subtracting 6 from the parameter received) dynamically change the color as follows:

  • 0 : Blue
  • Negative value : Red
  • Positive value : Green

So, the output will look like this:

enter image description here

Activity:

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
      private RatingBar ratingBar;
      private TextView tvRating;
      private LayerDrawable stars;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.original_activity_main);

        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        tvRating = (TextView) findViewById(R.id.value);
        stars = (LayerDrawable) ratingBar.getProgressDrawable();

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

                int value = (int) (ratingValue) - 6;
                tvRating.setText(String.valueOf(value));

                int color = Color.BLUE;

                if(value > 0)
                    color = Color.GREEN;
                else if(value < 0)
                    color = Color.RED;

                    stars.getDrawable(2).setColorFilter(color, PorterDuff.Mode.SRC_ATOP);    
            }
        });
      }

    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result : " />

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/label"
        android:text="" />

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/label"
        android:isIndicator="false"
        android:numStars="11"
        android:rating="0.0"
        android:stepSize="1.0" />

</RelativeLayout>
Infinite Recursion
  • 6,315
  • 28
  • 36
  • 51
1

As far as I know, the minimum value you can set the RatingBar is 0 (no negative numbers).

If you set the stepSize to 1.0, then you would have to set the ratingbar using an if condition.

For example:

if(yourNumber ==(-5)){
ratingBar.setRating(1.0f); //you need to set it using a Float value
} else if (yourNumber ==(-4)){
ratingBar.setRating(2.0f);
} //And so on....

About changing the star colors - you need to define your own Ratingbar style - read this post.

Good luck!

Community
  • 1
  • 1
gilonm
  • 1,719
  • 1
  • 10
  • 22