Star Rating Bar In Android Using Android Studio

Introduction

 
In this article, we are going to see how to obtain a rating bar in Android using Android Studio. Rating Bar is a famous library used widely all over the world. There are several types of rating bars but we will use the Star rating bar.
 
 Step 1
 
Create a new project in Android Studio.
 
Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity name and click on "Finish".
 
Android
 
Step 2
 
Next, copy the star image in .png format
 
Android
 
Locate app>>res>>drawable folder and paste the copied image into the drawable folder.
 
Android
 
Check whether it is pasted correctly.
 
Android
 
Step 3
 
Locate the Gradle Scripts>>Build.Gradle 1 
 
Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Code copy is here:
  1. maven { url 'https://jitpack.io' }
Step 4
 
Locate the Gradle Scripts>>Build.Gradle 2
 
Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Code copy is here
  1. compile 'com.github.ome450901:SimpleRatingBar:1.4.1'
Step 5
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
 
Android
 
And just type the code as follows.
 
Android
 
Code copy is here:
  1. <com.willy.ratingbar.ScaleRatingBar  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.    android:id="@+id/simpleRatingBar"  
  4.    android:layout_width="400dp"  
  5.    android:layout_height="100dp"  
  6.    app:srb_numStars="5"  
  7.    app:srb_minimumStars="1"  
  8.    app:srb_rating="2"  
  9. android:layout_centerInParent="true"  
  10.    app:srb_starWidth="40dp"  
  11.    app:srb_starHeight="50dp"  
  12.    app:srb_starPadding="15dp"  
  13.    app:srb_stepSize="0.5"  
  14.    app:srb_isIndicator="false"  
  15.    app:srb_clickable="true"  
  16.    app:srb_scrollable="true"  
  17.    app:srb_drawableEmpty="@drawable/empty"  
  18.    app:srb_drawableFilled="@drawable/full">  
  19. </com.willy.ratingbar.ScaleRatingBar>  
Step 6
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
 
Android
 
And check whether it is default or else make it default.
 
Android
 
Step 7
 
Verify the preview.
->After the code is applied, the preview will appear like this.
 
Android
 
Step 8
 
Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code
 
The code used in the activity_main.xml is used to insert the rating bar in our app and it is also used to define its width, height, and gravity of the rating bar.
 

Summary

 
We created a new app named rating bar and placed the rating icons in a drawable folder. After that, we have inserted the Gradle dependencies to build Gradle and verified the deployed output.
 
*Support and Share, Thank You*


Similar Articles