Android RatingBar Example

By | July 29, 2013
Android SDK Provides  “android.widget.RatingBar” class to create rating bar component with start icon , The user can touch or drags to set the rating .

In this tutorial we will see how to add ratingbar to the activity and check the change in the state of ratingbar when the us touches or drags over it using the “onRatingChanged()” method

Android Rating Bar Android Rating Bar on Rate Change

Lets See An Example

Project Detail

Project Name RatingBar
Package com.example.ratingbar
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

 file:- activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15dp" >

<RatingBar
android:id="@+id/rateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />

<TextView
android:id="@+id/rateStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Result:"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

2. Activity

 file:- MainActivity.java

package com.example.ratingbar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnRatingBarChangeListener {

RatingBar R_Bar;
TextView R_status;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
R_Bar = (RatingBar) findViewById(R.id.rateBar);
R_status = (TextView) findViewById(R.id.rateStatus);
R_Bar.setOnRatingBarChangeListener(this);
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
R_status.setText("Result : " + rating);

}
}


Android Rating Bar Example