Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get RatingBar value

Tags:

android

how to read and display rating bar value

i[0] // should be selected value

private OnClickListener onclickbutton1 = new OnClickListener() {
    public void onClick(View v) {
        int[] i = new int[]{ R.id.mRatingBar};

        statusMessage.setText("value is " + i[0]);
    }
};

// this works

private OnClickListener onclickbutton1 = new OnClickListener() {
    public void onClick(View v) {
        RatingBar mBar = (RatingBar) findViewById(R.id.mRatingBar);

        float[] i = new float[]{ mBar.getRating() };

        statusMessage.setText("value is.. " + i[0]);
    }
};
like image 554
User6996 Avatar asked Sep 07 '11 10:09

User6996


4 Answers

Simple call

  ratingBar.getRating() ; GET RATING BAR VALUE

When you want to get the rating value when changed

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

     final int numStars = ratingBar.getNumStars();
     ratingBar.getRating() ;
     final float ratingBarStepSize = ratingBar.getStepSize();

}
like image 61
bHaRaTh Avatar answered Oct 19 '22 22:10

bHaRaTh


What you do here is not right: you output the resource id of the rating bar, not its value.

Let me assume that you have earlier done something like:

RatingBar mBar = (RatingBar) findViewById(R.id.mRatingBar);
mBar.setOnClickListener(onclickbutton1);

for example in the activity's onCreate(). Then within the on click listener you provide, you can get the rating as follows:

public void onClick(View v) {
    RatingBar bar = (RatingBar) v;
    statusMessage.setText("value is " + bar.getRating());
}
like image 40
marc1s Avatar answered Oct 19 '22 23:10

marc1s


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

                Float ratingVal = (Float) rating;
                Float ratingvalue = (Float) rbRatingBar.getRating();
                Toast.makeText(getApplicationContext(), " Ratings : " + String.valueOf(ratingVal) + "", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), " Ratings1 : " + ratingvalue + "", Toast.LENGTH_SHORT).show();
            }
        });
like image 2
Deepak Rajput Avatar answered Oct 19 '22 22:10

Deepak Rajput


use this to get rating

(RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
        ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
    }

    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
        final int numStars = ratingBar.getNumStars();
        mRatingText.setText( 
                getString(R.string.ratingbar_rating) + " " + rating + "/" + numStars);

 }
like image 2
Nikunj Patel Avatar answered Oct 19 '22 22:10

Nikunj Patel