Sunday, October 31, 2010

Android RatingBar demo program



Android RatingBar example

Download SuperStar.zip
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidorigin.demos"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" 
          android:label="@string/app_name">
        <activity android:name=".SuperStarActivity"
                  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>
SuperStarActivity.java
/**
 *  Source: http://androidorigin.blogspot.com
 *  Author: http://tamilcpu.blogspot.com 
 */
package com.androidorigin.demos;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class SuperStarActivity extends Activity implements OnRatingBarChangeListener {
 
 protected TextView lblRating;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  ToggleButton toggleButton = (ToggleButton) findViewById(R.id.ToggleButton01); 
  final RatingBar myRatingBar = (RatingBar)findViewById(R.id.RatingBar01);  
  lblRating = (TextView)findViewById(R.id.TextView01);
  
  toggleButton.setChecked(true);
  
  // Set appropriate listener to listen required events
  myRatingBar.setOnRatingBarChangeListener(this);
  toggleButton.setOnClickListener(new View.OnClickListener() {
    
   public void onClick(View v) {
    if (((ToggleButton) v).isChecked()) {
     DisplayToast("Toggle button is On");
     myRatingBar.setEnabled(true);
     lblRating.setText("Rating is " + myRatingBar.getRating());
    }
    else {
     DisplayToast("Toggle button is Off");
     lblRating.setText("");
     myRatingBar.setEnabled(false); // disable ratings bar
    }
   }
  });  
 }
 
 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
  lblRating.setText("Rating is " + rating);  
 }
 
 private void DisplayToast(String msg) {
  // Toast widget displays small messages
  Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
 }
} 




2 comments:

  1. thanx a lot man!!

    great way of describing example its helped me a lot..

    keep this continue thanks again...

    ReplyDelete
  2. This is nice post!! Its help me lot and this link
    http://mindstick.com/Articles/f474917a-ed19-41d9-9a73-c537013b716b/?Rating%20Bar%20control%20in%20Android

    also helped me to complete my task.

    Thanks

    ReplyDelete