• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 3 comments


SIMPLE RATINGBAR

SOURCE CODE [main.xml] is

<?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" >

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
   
<RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
   
                <TextView android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
   
</LinearLayout>

SOURCE CODE [RatingBarExample.java] is

package com.RatingBarExample;

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

public class RatingBarExample extends Activity
{

                RatingBar ratingbar;
                TextView ratings;

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

                                ratings = (TextView) findViewById(R.id.rating);
                                ratingbar = (RatingBar) findViewById(R.id.ratingbar);

                                ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
                                                public void onRatingChanged(RatingBar ratingBar, float rating,
                                                                                boolean fromUser)
{
                                                                // TODO Auto-generated method stub
                                                                ratings.setText("The Rating is " + rating);
                                                }
                                });
                }
}

The OUTPUT will be

https://lh5.googleusercontent.com/_xnrF0YSUul8/Tb-Vkpz70LI/AAAAAAAAAJI/RNnXgVY1yN0/ratingbar1.png

https://lh4.googleusercontent.com/_xnrF0YSUul8/Tb-VlEhsoLI/AAAAAAAAAJM/NqgawsD7pYA/ratingbar2.png

3 Responses so far.

  1. amit suri says:

    How to show top rated items into another activity using Custom ListView Rating Bar

  2. Mayaa PN says:

    Good one.. Thanks man!! this http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html was also good, have a look, may help..

Leave a Reply