Android Tutorial on Rating Bar

 

Android Ratingbar :

Android ratingbar is used to display the quality of a product in many fields to give a user experience in that particular domain or simply to rate the work, movie, products, and many more in android ratingbar example.

Ratingbar will be helpful to know the product rating much easily based on 5 stars easy to identify.

We can also accept ratings using a android ratingbar we will be covering the same in this tutorial how to accept and display ratings.

In real life almost every category contains rating bar which will help user to choose better products out of available.

Here is how a rating bar looks like

 

android ratingbar android ratingbar ratingbar ratingbar

 

 

So Today we will try to learn how to code rating bar in your app’s i.e, –> Rating Bar in Android App Coding.

 

In this tutorial on android ratingbar i will be showing rating bar in two different ways i.e,

  1. Static Rating Bar (i.e., predefined rating is shown)
  2. Dynamic Rating Bar (i.e., dynamic rating bar is shown)

 

With the above introduction i think you got a basic knowledge regarding What is Rating Bar ? and use of it.

Android ratingbar video tutorial :

Go through below android rating bar tutorial for more updates

 

 

activity_main.xml :

Adding Rating Bar to xml file

 

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

 

I have used two rating bars one for dynamic and other static.

Using dynamic rating bar user can input rating and using Static rating is shown.

 

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rating Bar"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dynamic Rating Bar"
        android:textColor="#238922"
        android:layout_marginTop="40dp"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
         />

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

    <View
        android:layout_width="300dp"
        android:layout_height="0.1dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:background="#000000" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="  enter rating..."
        android:id="@+id/editText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply Rating"
        android:layout_marginTop="30dp"
        android:id="@+id/applyRating" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Rating Bar"
        android:textColor="#238922"
        android:layout_marginTop="40dp"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
        />

    <RatingBar
        android:id="@+id/showrating"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="5"
        />


</LinearLayout>

MainActivity :

Here we have made rating bar to accept number and will check whether it is between 1 – 5 and if user entered value is greater than 5 it will remain in 5 Rating and if less than 0 it will be 0 Rating.

 

package ratingbar.androidcoding.abhishek.ratingbar;

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


public class MainActivity extends Activity
{
    RatingBar ratingBar,showrating;

    EditText editText;

    Button applyRating;

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

        ratingBar=(RatingBar)findViewById(R.id.ratingBar);
        showrating=(RatingBar)findViewById(R.id.showrating);
        editText=(EditText)findViewById(R.id.editText);

        applyRating=(Button)findViewById(R.id.applyRating);

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

                if(editText.getText().toString().matches("")){

                    Toast.makeText(getApplicationContext(),"Enter a number between 1 - 5 to apply rating ",Toast.LENGTH_LONG).show();


                }else{

                int a = Integer.valueOf(editText.getText().toString());

                    if(a>5){

                        a = 5;

                        Toast.makeText(getApplicationContext(),"Max Rating is --> 5 ",Toast.LENGTH_LONG).show();


                        showrating.setRating(a);

                    }else if(a<0){

                        Toast.makeText(getApplicationContext(),"Min Rating is --> 0 ",Toast.LENGTH_LONG).show();

                        a = 1;

                        showrating.setRating(a);

                    }else{

                        showrating.setRating(a);

                    }

                }



            }
        });

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                                        boolean fromUser) {

                Toast.makeText(getApplicationContext(),"Rating : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

            }
        });

        showrating.setFocusable(false);



    }


}

 

Android ratingbar output :

This screen depicts the usage of android ratingbar

android ratingbar

 

If you are having any query in this tutorial on android ratingbar do let us know in the comment section below. If like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Recyclerview ListView

  In mobile applications a lot of data needs to be shown in form of a listview which will consume...

Close