Sunday, October 31, 2010

Android RatingBar demo program

Android RatingBar example

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android=""
    <application android:icon="@drawable/icon" 
        <activity android:name=".SuperStarActivity"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
 *  Source:
 *  Author: 
package com.androidorigin.demos;

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. */
 public void onCreate(Bundle savedInstanceState) {
  ToggleButton toggleButton = (ToggleButton) findViewById(; 
  final RatingBar myRatingBar = (RatingBar)findViewById(;  
  lblRating = (TextView)findViewById(;
  // Set appropriate listener to listen required events
  toggleButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    if (((ToggleButton) v).isChecked()) {
     DisplayToast("Toggle button is On");
     lblRating.setText("Rating is " + myRatingBar.getRating());
    else {
     DisplayToast("Toggle button is Off");
     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();

Tuesday, October 19, 2010

Simple solution to get rid off Null pointer exception which occurs when trying to edit Android XML files in Eclipse Helios

How to get rid of this error message?

I faced off this annoying error messages too often whenever editing res/values/strings.xml.   At first I thought I have missed something with the Android SDK installation.  Later through googling I found many newbies like me loosing their hair.   : (
I found two solutions from internet for this problem,
  1. Right click the .xml file ---> Open with Android XML editor
  2. Adding namespace attribute to root tag of our xml file
<resources xmlns:android="" >
Second method worked for me and i hope the same for you also.

I found one more solution with my click-anything experimentation : ) 
Click the Resources/Structure tab and just press [s] icon in top right corner. Voila it worked for me.

If it worked for you also, don't forget to share your happiness.  Have a happy computing.