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]);
}
};
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();
}
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());
}
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();
}
});
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With