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

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

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