Rating Bar in Android

Ever since the boom of the internet, there has been a massive increase in products on it. With the conveniences that internet has provided to the world, everyone wants to promote their products through it to reach a wider audience without spending too much. For this reason, marketing has become a huge part of the internet. As a matter of fact, it is very rare these days to see a page on the internet that does not have an advertisement.

Of course, everyone wants to highlight only the good side of their products even if the product is overflowing with flaws. In fact, there are more faulty products on the internet than high quality ones. In such a situation, a rating system becomes a necessity. With a proper rating system, a potential consumer can easily distinguish between a good product and a bad product.

In Android, the Rating Bar tool is used to achieve this purpose. To educate our readers, this article will be a comprehensive guide of Rating Bar, starting from the basics to creating a Rating Bar from scratch.

Rating Bar in Android

In order to enable people on the internet to rate a product, Android introduced a brilliant graphic solution in the shape of its Rating Bar option. By nature, a Rating Bar is an extension of Progress Bar and Seek Bar. A Rating Bar shows a rating with the help of stars. A Rating Bar in its default state allows a user to touch, drag or use arrow keys to enter their rating. A floating point number is returned whenever a value of rating is entered. This number may be like 1.0, 3.5, 4.5 and so on.

Examples of Rating Bar in Android

Because it has numerous applications, the Rating Bar has become one of the most widely used tools in Android applications.

An example of a Rating Bar is always present in your Android devices. This popular Rating Bar can be found inside Google Play Store. When you open Google Play Store and tap on any app present on its main activity, a new page opens.

This page shows you details about the app you selected, such as user reviews, number of downloads, an “about” section, etc. It also shows a big-sized floating point number standing above 5 stars, showing the current average rating of the app. If you have this app already downloaded in your device, you would see a Rating Bar asking you to “rate this app.”

Apart from online shopping apps where Rating Bars are easily found, you can also see a Rating Bar in food delivery marketplace apps, such as foodpanda. Here, a Rating Bar is used to rate services of a restaurant. Usually, in these Android apps, Rating Bars for restaurants are placed on their respective pages.

Steps

Now that all the important points regarding Rating Bar in Android are covered, we will show you how to develop it for an Android app.

First off, you need to open the Android Studio IDE (Integrated development Environment), which is a platform that Android has provided for app development.

After launching Android Studio, create a new project as shown in our initial tutorials. Then, give an appropriate name to your project. For example, our project name is “Rating Bar.” Similarly, choose a name for your package as well. We have chosen “com.androidaura.ratingbar” as our package name.

Now, open the MainActivity.java class file in order to enter the following code:

// MainActivity.java

package com.androidaura.ratingbar;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    RatingBar rb;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rb = (RatingBar) findViewById(R.id.rating);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rated = "You Rated :" + rb.getRating();
                Toast.makeText(MainActivity.this, rated, Toast.LENGTH_SHORT).show();
            }
        });
    }
} 

When you are done coding in your MainActivity.java class file, open the second file of your project, i.e. activity_main.xml layout file. After opening this file, add the following code in it:

// activity_main.xml

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

    <RatingBar
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:saveEnabled="true"
        android:numStars="5"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout> 

After successfully entering both the above-mentioned codes in their respective files, you will be ready to launch your project and view the output. By using the codes we have shown, your output would look like the following illustration:

Output

Rating Bar in Android