Android – RatingBar example

By -
Problem: How to place rating bar and have rating value in android app?
Solution:

RatingBarDemoActivity.java

package com.technotalkative.ratingbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

public class RatingBarDemoActivity extends Activity {
    /** Called when the activity is first created. */

	RatingBar ratingBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
        ratingBar.setOnRatingBarChangeListener(new myListener());

    }

    class myListener implements RatingBar.OnRatingBarChangeListener
    {

		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			// TODO Auto-generated method stub
			Toast.makeText(RatingBarDemoActivity.this, "Rating => "+rating, Toast.LENGTH_SHORT).show();
		}
    }
}

main.xml

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

    <RatingBar
    	android:id="@+id/ratingBar1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:numStars="5"
	android:rating="2"
	android:stepSize="1">
    </RatingBar>
	<!-- `rating` is used to set pre-defined rating value -->
	<!-- `stepSize=='1' defines user can't give floating point rating -->
</linearLayout>

Download full example: Android – RatingBar example

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...