Thursday 28 February 2013

Rating Bar Demo

Demo







1)ratingbar.xml:

<?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:background="#EEA9B8"
    android:orientation="vertical" >

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Show Rating" />

</LinearLayout>


2)Activity 


package com.venky.ratingbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class RatingBarDemoActivity extends Activity {
      /** Called when the activity is first created. */

      RatingBar ratingBar;

      float ratingByUser;

      Button btnShowRating;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ratingbar);

            ratingBar = (RatingBar) findViewById(R.id.ratingBar);

            ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                  @Override
                  public void onRatingChanged(RatingBar ratingBar, float rating,
                              boolean fromUser) {
                        // TODO Auto-generated method stub

                        ratingByUser = rating;
                  }
            });

            btnShowRating = (Button) findViewById(R.id.button);

            btnShowRating.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Toast.makeText(getApplicationContext(),
                                    "User Rating =>" + ratingByUser, Toast.LENGTH_SHORT)
                                    .show();

                  }
            });
      }
}



No comments:

Post a Comment