RatingBar in Android UI Controls

ratingbarAndroid RatingBar is used to Accept and display the user rating. This Control shows the number of stars in which the user can touch/drag to set the value in Android, We can create the rating bar(rating stars) using the tag in xml UI Layout file.

eg:

<android:layout_width="wrap_content"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp" />
RatingBar

followings are the Properties of Android RatingBar control : a) android:isIndicator : this Attribute takes a boolean value either true or false, if this Property is set to true than the RatingBar will be non-changeable by the user, in other words the RatingBar will be Act like Indicator. b) android:numStars : this takes the integer value which indicates the number of stars to be displayed in the RatingBar control. c) android:raing : this takes a float value input which will be the default Rating, which is going to be the displayed in the rating bar control, when the RatingBar is loaded for first time. d) android:stepSize: this Attribute take a float value which is the step size of the rate(rating) increased or decried. for eg. if we set the step size to 1.0 than the rating value will be increased or decried by 1, and if have given step size of 0.5 than the rating size will be increased or decried by 0.5 .

xml file:-

<xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

android:numStars="7"
android:stepSize="0.25"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1" />


Java file:-

package com.coreprogrammers.ratingbardemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity
{

        /**
        * Rating bar instance
        */
        RatingBar ratingBar;
        /**
        * Customized Rating text
        */
        TextView ratingText;
        
        /**
        * @see android.app.Activity#onCreate(android.os.Bundle)
        */
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            ratingText = (TextView) findViewById(R.id.rating);
            
            ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
        
            ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
            {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar,floatrating, boolean fromUser)
                    {
                        final int numStars = ratingBar.getNumStars();
                        ratingText.setText(rating + "/" + numStars);
                    }
            });
        
        }

}
 

Java Code

    
        
Skip to toolbar