Android RatingBar with Examples

Android RatingBar,In android, Bar is a UI control that’s used to get the rating from the consumer. The Bar is an extension of SeekBar and ProgressBar that suggests a score in stars and it allow users to set the rating price by using touch or click at the stars.

The android Bar will usually go back a rating fee as floating factor variety including 1.0, 2.Zero, 2.Five, three.Zero, three.5, etc.

Following is the pictorial representation of the use of a Bar in android programs.

In android, by using the usage of android:numStars characteristic we will outline the number of stars to display in Bar. An instance of using Bar is in movie websites or product sites to collect the user score approximately the movies or merchandise, and many others.

In android, by means of the usage of android.Widget.Bar aspect we can display the score bar with megastar icons.

Android RatingBar,Create Android Bar in XML Layout File

Android RatingBar,In android, we can create Bar in XML layout file using <RatingBar> element with different attributes like as shown below.

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:rating="3.5"/>

If you observe above code snippet, we defined a rating bar (<RatingBar>) with different attributes, those are

Attribute Description
android:id It is used to uniquely identify the control
android:numStars It is used to define number of stars to display.
android:rating It is used to set the default rating value for ratingbar.

Now we will see how to get the rating value from Bar control in android applications.

Android RatingBar,Get Android Bar Value

Android RatingBar,In android, by using using Bar techniques (getNumStars(), getRating()) we can get the range of stars and the rating value which changed into selected.

Following is the code snippet to get the score info from Bar in android applications.

int noofstars = rBar.getNumStars();
float getrating = rBar.getRating();
tView.setText("Rating: "+getrating+"/"+noofstars);

This is how we can get the number of stars in Bar control and the selected rating value from Bar control in android applications.

Bar Control Attributes

Following are the some of commonly used attributes related to Bar control in android applications.

Attribute Description
android:id It is used to uniquely identify the control
android:numStars It is used to define number of stars to display.
android:rating It is used to set the default rating value for ratingbar.
android:background It is used to set the background color for progress bar.
android:padding It is used to set the padding for left, right, top or bottom of progress bar.

RatingBar Control Example

Following is the instance of defining a Bar manage, Button manage and TextView control in RelativeLayout to get the selected score value from Bar on Button click.

Create a brand new android application the usage of android studio and give names as RatingBarExample. In case if you aren’t aware of growing an app in android studio test this newsletter Android Hello World App.

Now open an activity_main.Xml record from reslayout route and write the code like as proven below

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="200dp"
        android:numStars="5"
        android:rating="3.5"/>
    <Button
        android:id="@+id/btnGet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ratingBar1"
        android:layout_below="@+id/ratingBar1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="60dp"
        android:text="Get Rating"/>
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnGet"
        android:layout_below="@+id/btnGet"
        android:layout_marginTop="20dp"
        android:textSize="20dp"
        android:textStyle="bold"/>
</RelativeLayout>

If you observe above code we created a one Bar control, one Button and one TextView manipulate in XML Layout file.

Once we’re accomplished with introduction of layout with required controls, we need to load the XML layout aid from our pastime onCreate() callback method, for that open fundamental activity report MainActivity.Java from javacom.Tutlane.Ratingbarexample path and write the code like as shown below.

MainActivity.java

package com.tutlane.ratingbarexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RatingBar rBar;
    private TextView tView;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rBar = (RatingBar) findViewById(R.id.ratingBar1);
        tView = (TextView) findViewById(R.id.textview1);
        btn = (Button)findViewById(R.id.btnGet);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int noofstars = rBar.getNumStars();
                float getrating = rBar.getRating();
                tView.setText("Rating: "+getrating+"/"+noofstars);
            }
        });
    }
}

If you have a look at above code we are calling our layout the usage of setContentView technique inside the form of R.Layout.Layout_file_name in our interest record. Here our xml file name is activity_main.Xml so we used report call activity_main and we are trying to get the number of stars in Bar and the chosen score fee from Bar manage.

Generally, all through the release of our pastime, onCreate() callback technique may be known as by using android framework to get the desired layout for an hobby.

Output of RatingBar Example

When we run above example using android virtual device (AVD) we will get a result like as shown below.

If you look at above result, we’re capable of get the score value from the Bar control whilst we click on on Button in android software.

This is how we are able to use Bar manage in android programs to expose the scores based totally on our requirements.