Android RatingBar Control

If you want need to rate a movie or website i.e. rating in stars, then use the Android RatingBar control. It is the subclass of android.widget.AbsSeekBar,

Usage

Use the Android RatingBar control to show movie reviews, website rating, and product reviews etc.

Android RatingBar Control

Attributes

Here are the attributes of RatingBar Android Control,

RatingBar AttributeDescriptionValue
android:isIndicatorWhether this rating bar is an indicator.true or false
android:numStarsNumber of stars, visible to users.Integer Value
android:ratingSet the default rating with this attribute.Floating Point Value
android:stepSizeSet the step size of the rating.Floating Point Value

Example

MainActivity.java

The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.

On button click, a Toast will appear with a message showing what rating you have given to the movie using the RatingBar. Toast in Android is a notification message visible in the form of popup.

Weā€™re using getRating() method to retrieve the value of rating. Ratings under RatingBar have floating point value, for example, 2.0, 3.0, 5.0, etc.

activity_main.xml

For src\main\res\layout\activity_main.xml,

Weā€™ll add our UI content here, 2 TextView, 1 Button and 1 RatingBar. On button click, the rating you entered using RatingBar will get displayed. For displaying the rating, weā€™ve used Toast,

AndroidManifest.xml

The content of src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,

strings.xml

Hereā€™s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible. We hardcoded string ā€œStudyopedia RatingBar Applicationā€ and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>.

We also hardcoded the review string and used @string/review and result string with @string/result,

Output

We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + f10. This will install your app on the AVD and the following emulator with your app opens up.

Hereā€™s the output, first screen visible,

Android RatingBar Control Output1

Give a rating to the movie and click the RESULT button.

Android RatingBar Control Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Android ToggleButton Control
Android CheckBox Control
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading