RatingBar Tutorial With Example In Android Studio

How to used Review Rating Bar in android app

In this article I am sharing to how to used a rating bar in Android  How to implement in Android and how to get rating number with a star rating. these I a very simple way to implement the rating.

Rating bar 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.

 

Step 1: Start Android Studio and Create a Project in your Android studio

Setp2:-  Create an Activity CustomerFeedback

Step3:- Open Your activity_customer_feefback.xml

activity_customer_feefback.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="codeplayon.com.CustomerFeedback">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="20dp"
                android:layout_weight="1"
                android:textColor="@color/colorAccent"
                android:text="Company"/>
            <RatingBar
                android:id="@+id/ratingBar1"
                android:theme="@style/RatingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="20dp"
                android:layout_weight="1"
                android:textColor="@color/colorAccent"
                android:text="Customer\n care"/>
            <RatingBar
                android:id="@+id/ratingBar2"
                android:theme="@style/RatingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="20dp"
                android:layout_weight="1"
                android:textColor="@color/colorAccent"
                android:text="Service     "/>
            <RatingBar
                android:id="@+id/ratingBar3"
                android:theme="@style/RatingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp" />

        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="bottom"
            android:textColor="@color/colorAccent"
            android:textSize="18dp"
            android:text=" Any Suggestion"/>


        <EditText
            android:id="@+id/FeedMessage"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:gravity="top"
            android:padding="10dp"
            android:background="@drawable/editbox"/>

        <Button
            android:id="@+id/sendFeed"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="@color/white"
            android:background="@drawable/button"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:textStyle="italic"
            android:text="Send"/>

    </LinearLayout>

</LinearLayout>

Step 5:- Open Your Java File and implement it

public class CustomerFeedback extends AppCompatActivity {
    
    EditText FeedMessage;
    Button SendFeed;
    String meassage="";
    String rating1="",rating2="", rating3="";
    RatingBar ratingbar1,ratingbar2,ratingbar3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_feedback);
   
        FeedMessage=(EditText)findViewById(R.id.FeedMessage);
        ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
        ratingbar2=(RatingBar)findViewById(R.id.ratingBar2);
        ratingbar3=(RatingBar)findViewById(R.id.ratingBar3);

        SendFeed=(Button)findViewById(R.id.sendFeed);
        SendFeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               
                    rating1=String.valueOf(ratingbar1.getRating());
                    rating2=String.valueOf(ratingbar2.getRating());
                    rating3=String.valueOf(ratingbar3.getRating());

        });     
    }
}

 

 

 

Facebooktwitterredditpinterestlinkedinmail
Recommended
 Image slider in Android Slider Layout Hey in these I…