What is ratingbar

Android RatingBar is used to get the rating from the user. It display the rating in the form of stars.The user can touch , drag or click to give the rating.The rating is always in floating values like 1.0,2.0 etc.

The best example of RatingBar is when you make the whatsapp call after completion of call whatsapp display a RatingBar for the user to give rating for feed back of call.


RatingBar:.

In this section i am going to show the RatingBar with practical example.

First open the android studio and create a project and give the name of your project like RatingBarDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one RatingBar and one Button and one TextView. Open the activity_main.xml file and code look like this


<RelativeLayout 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="wrap_content"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="123dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="264dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="41dp"
        android:layout_marginRight="41dp"
        android:layout_marginTop="42dp"
        android:layout_toLeftOf="@+id/button"
        android:layout_toStartOf="@+id/button"
        android:text="Rating" />
</RelativeLayout>

       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;//you must add this line other wise it gettting error.
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    RatingBar ratingBar;
    TextView textView;

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


    }
    public void onClickButton(View v)
    {
        String rate=String.valueOf(ratingBar.getRating());
        textView.setText(rate);

    }

}


       

Explanation:

RatingBar ratingBar:This statement create the object ratingbar of RatingBar.

TextView textView:This statement create the object textView of TextView.

ratingBar=(RatingBar)findViewById(R.id.ratingBar):This statement initialized the ratingBar object.

textView=(TextView)findViewById(R.id.textView):This statement initialized the textView object.

After initialization i have to created the onClickButton().

String rate=String.valueOf(ratingBar.getRating()):The getRating() return the rating in floating values after converting it into String value assign into the rate variable.

textView.setText(rate):This statement set the rate value on textView object.

textView.setText(buffer.toString());This statement set the buffer value into textView object using setText().

You do not forget to set the android:onClick="onClickButton".If you do not set this property button will not perform any action .


You can also write this code for direct click on RatingBar and it will display the rating value into the textView.

You need to drag and drop one TextView and one RatingBar. And write this code


package com.example.niitpc.myapplication;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;//you must add this line other wise it gettting error.
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    RatingBar ratingBar;
    TextView textView;

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

                new RatingBar.OnRatingBarChangeListener() {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                        String rate=String.valueOf(rating);
                        textView.setText(rate);
                    }
                }
        );


    }

}
     

RatingBar

What is RatingBar for video Click here

Smiley face                     Smiley face