In this tutorial we will learn about how to implement a RatingBar in android, how to getRating() and show a toast message of rating value when user submit it and their example .

A RatingBar is a kind of or we can say it is an extension of ProgressBar and SeekBar that display ratings in stars. RatingBar is used to get the ratings from user for an application .

The user can just touch the stars to set the rating or to provide the rating of an app . In RatingBar when user click on stars and submit it then it converted into our format or a float value .

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a button to your main layout file :

We create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/ratingBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Rate Us"/>

</RelativeLayout>

3. Create a Layout Resource file for Alert Dialog and implement a RatingBar in android xml layout :

Now create a Layout Resource file for custom alert dialog inside res ->layout and named it as rating.xml and create a RatingBar on custom alert dialog to display it for better user experience or just add the following code to rating.xml file .

rating.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"/>

</RelativeLayout>

4. Now open your MainActivity.java and add following code to handle getRating() method :

Now set the custom layout to alert dialog and handle the event of RatingBar to get the rating from user and show a toast message of converted value of their rating in MainActivity.java file .

MainActivity.java  :

package net.technxt;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    Button ratingBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBtn = (Button)findViewById(R.id.ratingBtn);

        ratingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                try {

                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    View layout= null;
                    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    layout = inflater.inflate(R.layout.rating, null);
                    final RatingBar ratingBar = (RatingBar)layout.findViewById(R.id.ratingBar);
                    builder.setTitle("Rate Us");
                    builder.setMessage("Thank you for rating us , it will help us to provide you the best service .");
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Float value = ratingBar.getRating();
                            Toast.makeText(MainActivity.this,"Rating is : "+value,Toast.LENGTH_LONG).show();
                        }
                    });
                    builder.setNegativeButton("No,Thanks", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setCancelable(false);
                    builder.setView(layout);
                    builder.show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });      
        
    }                          
}

Run the application to launch Android emulator or in your android device to test RatingBar in android and verify the result of the changes done in the application.

%d bloggers like this: