Recipe 2.5 Implementing a Star Rating System

Android Versions
Level 1 and above
Permissions
None
Source Code to Download from Wrox.com
CheckBoxesRating.zip

The previous recipe introduced you to the checkbox, in particular the star checkbox. One popular use of the star checkbox is to implement a star rating system, whereby users tap on the stars to provide a rating for a particular item (such as user reviews for books, music, or venues). This recipe demonstrates how to implement this rating system.

Solution

Assume you have the following code snippet in the activity_main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:id="@+id/ratings" > <CheckBox android:id="@+id/star1" style="?android:attr/starStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="1" /> <CheckBox android:id="@+id/star2" style="?android:attr/starStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="2" /> <CheckBox android:id="@+id/star3" style="?android:attr/starStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="3" /> <CheckBox android:id="@+id/star4" style="?android:attr/starStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="4" /> <CheckBox android:id="@+id/star5" style="?android:attr/starStyle" ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.