RatingBar is used to allow the users to rate some products. In the below code getRating() function is used to calculate the rating of the products. The getRating() function returns double type value.
Below steps are involved to create a RatingBar in Android:
- Create a new android project.
- Add RatingBar in your activity_main.xml.
- Add Button to invoke action.
- Use TextView to display the ratings.
- To use the rating bar in the app, we will use the in-built RatingBar widget, hence the first step is to import it into the project.
- In the MainActivity, make the RatingBar object denoted by the variable ‘rt’ and find its corresponding view in the XML file. This is done by the findViewById() method. After the java object has successfully bind to its view, create the ‘stars’ layout, which the user will interact with, to set the rating.
- To get the drawable stars, the method rt.getProcessDrawable() is used. Then to modify the colours of the stars, the method setColorFilter() is used and the argument Color.YELLOW is passed. Finally, the Call method is written to extract the value of the rating that the user has selected, by the method rt.getMethod().
Program to create MainActivity:
Note: For the layout, ConstraintLayout is good to use if you are a beginner because it can adjust the views as per the screens.
This XML file defines the view of the application.
Program to create layout for MainActivity:
Here we don’t need to change the manifest file, no permission is required for the ratingBar. By default, all the created new activities are mentioned in the manifest file.
Below is the code for AndroidManifest.xml
- Android | Creating a SeekBar
- Creating a Calculator for Android devices
- Android | Creating a Calendar View app
- Android | Creating multiple Screen app
- Android | Creating a Splash Screen
- Dynamic RatingBar in Kotlin
- Android | How to Create/Start a New Project in Android Studio?
- Android | Android Application File Structure
- Android | How to Request permissions in Android Application?
- Android | AdMob Banner Ads for Android Studio
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to add Radio Buttons in an Android Application?
- Android | Running your first Android app
- Creating a file using FileOutputStream
- Creating Frames using Swings in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.