RatingBar in Android

In this tutorial we will learn how to use RatingBar in Android. RatingBar is use to get rating from users. User can give rating by clicking on starts. When user click on starts of Ratingbar it returns float value like 2.5, 1.0, 4.5 etc. In this tutorial we will learn how to add Ratingbar in XML layout file. How to get Rating in Java file.

RatingBar in Android

First of all we will write our layout file code. In layout file we will add a RatingBar widget and a button. After that we will write code in Java file to get rating given by user on button click.Check this XML code.

XML Example: -
<?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:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:saveEnabled="true" android:numStars="5" android:layout_marginBottom="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:id="@+id/submit"/> </LinearLayout>

After writing this code we will write code in our java file to fetch rating given by user.


public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

       final RatingBar ratingBar=findViewById(R.id.ratingbar);
       final Button button=findViewById(R.id.submit);

       button.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               float rating=ratingBar.getRating();

Here is the output after writing this code.

Ratingbar in Android

Share this tutorial with your friends on your social Media..

RatingBar in Android RatingBar in Android RatingBar in Android RatingBar in Android

Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.