Android program to implement Rating Bar

Android program to implement Rating Bar

Rating Bar in Android is a subclass of the SeekBar class. It is used to get the rating from the user. It displays the rating in stars. For example, whenever you download an android app, once you visit the app, then it asks you to rate the app, this is the use of rating bar widget. Rating bar returns a floating-point number it can be 2.0, 3.5, 4.0 or some other number.

To get the value of rating from the user, we need to use the getRating() method.

In the code given below, we will be using the rating bar and displaying the value of rating assigned in the textview onclick of the “rate us” button.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “RatingBar” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the contents of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codedost.ratingbar.MainActivity">

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:id="@+id/ratingBar" />

    <Button
        android:text="RATE US"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ratingBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="87dp"
        android:id="@+id/textView" />
</RelativeLayout>

MainActivity.java

package codedost.ratingbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    RatingBar rb;
    Button b1;
    TextView tv;

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

        rb=(RatingBar) findViewById(R.id.ratingBar);
        b1=(Button) findViewById(R.id.button);
        tv=(TextView) findViewById(R.id.textView);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tv.setText(String.valueOf(rb.getRating()));
            }
        });
    }

Output

Share Me!