Android: Creating a simple RatingBar

19 September 2012 By Nithya Vasudevan 3,372 views 2 Comments
6 Flares Twitter 0 Facebook 0 Google+ 6 6 Flares ×

Project Description

In this Android rating bar tutorial, we will create a simple rating bar using android.widget.RatingBar defined in XML layout file and implement OnRatingBarChangeListener to accept user rating and display it as a Toast message.

Environment Used

Prerequisites

Android Project

Create an Android project and name it as “SimpleRatingBar“.

strings.xml

Open res/values/string.xml and replace it with following content.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, SimpleRatingBarActivity!</string>
    <string name="app_name">SimpleRatingBar</string>
</resources>

XML layout files

This application uses XML layout file (main.xml) to display the rating bar.

Open main.xml file in res/layout and copy the following content.

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

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

This layout file creates a new Android RatingBar.

android:numStarsThe number of stars (or rating items) to show.
android:stepSizeThe step size of the rating.

Activity

In src folder, create a new Class and name it as “SimpleRatingBarActivity” in the package “com.theopentutorials.android.views” and copy the following code.

package com.theopentutorials.android.views;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class SimpleRatingBarActivity extends Activity implements
		OnRatingBarChangeListener {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
		ratingbar.setOnRatingBarChangeListener(this);
	}

	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		Toast.makeText(SimpleRatingBarActivity.this, "New Rating: " + rating,
				Toast.LENGTH_SHORT).show();
	}
}

Here, the rating bar is registered to the android.widget.RatingBar.OnRatingBarChangeListener using the method ratingbar.setOnRatingBarChangeListener(). When the event (user clicking on rating bar) occurs it calls onRatingChanged() method. For simplicity, we display a Toast message with user’s rating.

AndroidManifest.xml

Define the activity in AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.theopentutorials.android.views"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SimpleRatingBarActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Run your application

Project Folder Structure

The complete folder structure of this example is shown below.

Tags: , , , , ,

  • coco

    i got these error

    ERROR: Application requires API version 15. Device API version is 7 (Android 2.1-update1).

  • Uday

    Shader ‘android.graphics.BitmapShader’ is not supported in the Layout Editor.
    i got this error can u give me solution…