RatingBar Introduction:-

RatingBar is used to get the rating from the app user. A user can simply touch, drag or click on the stars to set the rating value. The value of rating always returns a floating point number which may be 1.0, 2.5, 4.5 etc.

Following are the important attributes of a RatingBar.

  • android:numStars : The number of stars to show in the RatingBar
  • android:stepSize : The step size of the rating. A size of 0.5 implies half ratings can be set (such as 3.5)
  • android:isIndicator : Whether this rating bar is an indicator that indicates the total number of ratings and is non-changeable by the user
  • style=”?android:attr/ratingBarStyleSmall” : Creates small indicator RatingBar style

The getRating() method of RatingBar returns the rating number.
OnRatingBarChangeListener interface is implemented and the following method needs to be overridden:


Step 1:-

create a new project and name it RatingBarExample.

Select File->New ->New Project and Fill the forms and click "Finish" button.


Step 2:-

open activity_main.xml and add the below code :-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rating Bar"
android:textSize="20dp"
android:gravity="center"></TextView>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Get Rating"></Button>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result:- "
android:textColor="#000"
android:textSize="18dp"></TextView>
</LinearLayout>

Step 3:-

open MainActivity.java and add the following Code:-

MainActivity.java
package com.androidcreation.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.TextView;

public class MainActivity extends AppCompatActivity {

RatingBar ratingBar;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar=findViewById(R.id.ratingBar);
button=findViewById(R.id.button);
textView=findViewById(R.id.textview);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String rating="Rating :-"+ratingBar.getRating();
textView.setText(rating);
}
});
}
}


Output:-


Next :-

0 CommentsClose Comments

Leave a comment

shares

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)