to top
Android APIs
public class

RatingBar

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

Class Overview

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 ( ratingBarStyleSmall) and the larger indicator-only style (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
Attribute Name Related Method Description
android:isIndicator setIsIndicator(boolean) 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. 
[Expand]
Inherited XML Attributes
From class android.widget.ProgressBar
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
RatingBar(Context context, AttributeSet attrs, int defStyle)
RatingBar(Context context, AttributeSet attrs)
RatingBar(Context context)
Public Methods
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 onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
void setIsIndicator(boolean isIndicator)
Whether this rating bar should only be an indicator (thus non-changeable by the user).
synchronized void setMax(int max)

Set the range of the progress bar to 0...

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
synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

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

[Expand]
Inherited Methods
From class android.widget.AbsSeekBar
From class android.widget.ProgressBar
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:isIndicator

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

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol isIndicator.

Related Methods

android:numStars

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

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol numStars.

Related Methods

android:rating

The rating to set by default.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol rating.

Related Methods

android:stepSize

The step size of the rating.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol stepSize.

Related Methods

Public Constructors

public RatingBar (Context context, AttributeSet attrs, int defStyle)

Added in API level 1

public RatingBar (Context context, AttributeSet attrs)

Added in API level 1

public RatingBar (Context context)

Added in API level 1

Public Methods

public int getNumStars ()

Added in API level 1

Returns the number of stars shown.

Returns
  • The number of stars shown.

public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()

Added in API level 1

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

public float getRating ()

Added in API level 1

Gets the current rating (number of stars filled).

Returns
  • The current rating.

public float getStepSize ()

Added in API level 1

Gets the step size of this rating bar.

Returns
  • The step size.

public boolean isIndicator ()

Added in API level 1

Related XML Attributes
Returns
  • Whether this rating bar is only an indicator.

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void setIsIndicator (boolean isIndicator)

Added in API level 1

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

Related XML Attributes
Parameters
isIndicator Whether it should be an indicator.

public synchronized void setMax (int max)

Added in API level 1

Set the range of the progress bar to 0...max.

Parameters
max the upper range of this progress bar

public void setNumStars (int numStars)

Added in API level 1

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 The number of stars.

public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

Added in API level 1

Sets the listener to be called when the rating changes.

Parameters
listener The listener.

public void setRating (float rating)

Added in API level 1

Sets the rating (the number of stars filled).

Parameters
rating The rating to set.

public void setStepSize (float stepSize)

Added in API level 1

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

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

Protected Methods

protected synchronized void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Added in API level 1

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 overriden 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 horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.