RatingBar


public class RatingBar
extends AbsSeekBar

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.ProgressBar
       ↳ android.widget.AbsSeekBar
         ↳ android.widget.RatingBar


A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar. The smaller RatingBar style ( R.attr.ratingBarStyleSmall) and the larger indicator-only style (R.attr.ratingBarStyleIndicator) do not support user interaction and should only be used as indicators.

When using a RatingBar that supports user interaction, placing widgets to the left or right of the RatingBar is discouraged.

The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap content (if another layout width is set, the results may be unpredictable).

The secondary progress should not be modified by the client as it is used internally as the background for a fractionally filled star.

Summary

Nested classes

interface RatingBar.OnRatingBarChangeListener

A callback that notifies clients when the rating has been changed. 

XML attributes

android:isIndicator Whether this rating bar is an indicator (and non-changeable by the user). 
android:numStars The number of stars (or rating items) to show. 
android:rating The rating to set by default. 
android:stepSize The step size of the rating. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

RatingBar(Context context)
RatingBar(Context context, AttributeSet attrs)
RatingBar(Context context, AttributeSet attrs, int defStyleAttr)
RatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getNumStars()

Returns the number of stars shown.

RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener()
float getRating()

Gets the current rating (number of stars filled).

float getStepSize()

Gets the step size of this rating bar.

boolean isIndicator()
void setIsIndicator(boolean isIndicator)

Whether this rating bar should only be an indicator (thus non-changeable by the user).

void setMax(int max)

Set the upper range of the progress bar max.

void setNumStars(int numStars)

Sets the number of stars to show.

void setOnRatingBarChangeListener(RatingBar.OnRatingBarChangeListener listener)

Sets the listener to be called when the rating changes.

void setRating(float rating)

Sets the rating (the number of stars filled).

void setStepSize(float stepSize)

Sets the step size (granularity) of this rating bar.

Protected methods

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Inherited methods

XML attributes

android:isIndicator

Whether this rating bar is an indicator (and non-changeable by the user).

May be a boolean value, such as "true" or "false".

Related methods:

android:numStars

The number of stars (or rating items) to show.

May be an integer value, such as "100".

android:rating

The rating to set by default.

May be a floating point value, such as "1.2".

android:stepSize

The step size of the rating.

May be a floating point value, such as "1.2".

Public constructors

RatingBar

Added in API level 1
public RatingBar (Context context)

Parameters
context Context

RatingBar

Added in API level 1
public RatingBar (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

RatingBar

Added in API level 1
public RatingBar (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

RatingBar

Added in API level 21
public RatingBar (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

getAccessibilityClassName

Added in API level 23
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getNumStars

Added in API level 1
public int getNumStars ()

Returns the number of stars shown.

Returns
int The number of stars shown.

getOnRatingBarChangeListener

Added in API level 1
public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()

Returns
RatingBar.OnRatingBarChangeListener The listener (may be null) that is listening for rating change events.

getRating

Added in API level 1
public float getRating ()

Gets the current rating (number of stars filled).

Returns
float The current rating.

getStepSize

Added in API level 1
public float getStepSize ()

Gets the step size of this rating bar.

Returns
float The step size.

isIndicator

Added in API level 1
public boolean isIndicator ()

Related XML Attributes:

Returns
boolean Whether this rating bar is only an indicator.

setIsIndicator

Added in API level 1
public void setIsIndicator (boolean isIndicator)

Whether this rating bar should only be an indicator (thus non-changeable by the user).

Related XML Attributes:

Parameters
isIndicator boolean: Whether it should be an indicator.

setMax

Added in API level 1
public void setMax (int max)

Set the upper range of the progress bar max.

Parameters
max int: the upper range of this progress bar

setNumStars

Added in API level 1
public void setNumStars (int numStars)

Sets the number of stars to show. In order for these to be shown properly, it is recommended the layout width of this widget be wrap content.

Parameters
numStars int: The number of stars.

setOnRatingBarChangeListener

Added in API level 1
public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

Sets the listener to be called when the rating changes.

Parameters
listener RatingBar.OnRatingBarChangeListener: The listener.

setRating

Added in API level 1
public void setRating (float rating)

Sets the rating (the number of stars filled).

Parameters
rating float: The rating to set.

setStepSize

Added in API level 1
public void setStepSize (float stepSize)

Sets the step size (granularity) of this rating bar.

Parameters
stepSize float: The step size of this rating bar. For example, if half-star granularity is wanted, this would be 0.5.

Protected methods

onMeasure

Added in API level 1
protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.