
Android RatingBar is a type of SeekBar which is used to get rating from the users, Rating Bar give a floating result like 1.5, 2.0, 3.5, 4.5 etc. To make rating user have to touch or press the star shape.
Click here to read about RatiBar Alert Dialog
To get the value of RatingBar please use getRating() method. which returns a floating value as told above.
Android RatingBar Example:
Create a new Project and add a RatingBar and a Button with onClick method use below code to do it:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <RatingBar android:id="@+id/ratingBar" android:layout_width="240dp" android:layout_height="wrap_content"/> <Button android:id="@+id/btnSubmit" android:layout_width="150dp" android:layout_height="50dp" android:text="Submit" android:onClick="getRating" android:textSize="20sp"/> </LinearLayout>

Add the below code into your MainActivity.java file:
package com.nsc.ratingbar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; // Download Android Projects From :https://nulledsourcecode.com/ // Contact us for making custom Android App // and School or College Projects. WhatsApp: +91 8307878848 // Telegram: @AndroidStudioCode public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); Button button = (Button) findViewById(R.id.btnSubmit); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String numberOfRating = String.valueOf(ratingBar.getRating()); Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show(); } }); } public void getRating(View view) { } }
Final Example look:

Set threshold value to RatingBar:
You can also set a thresohld value to Android Rating Bar to which submit button will action means RatingBar will respond only when user gives you 4 or more stars, it is very useful method to avoide bad or less rating. You can achieve like that:
package com.nsc.ratingbar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; // Download Android Projects From :https://nulledsourcecode.com/ // Contact us for making custom Android App // and School or College Projects. WhatsApp: +91 8307878848 // Telegram: @AndroidStudioCode public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); Button button = (Button) findViewById(R.id.btnSubmit); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String numberOfRating = String.valueOf(ratingBar.getRating()); //Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show(); // below method will show action only when user gives rating 4 or high else it will show thankyou. float totalRating = Float.parseFloat(numberOfRating); if (totalRating >= 4){ Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"ThankYou!",Toast.LENGTH_SHORT).show(); } } }); } public void getRating(View view) { } }
Leave a Comment
You must be logged in to post a comment.