Thursday, 28 June 2012

Rate bar in android & Rate it,,,, with Source code

   Rate bar in android & Rate it,,,, with Source code

 

Here is an exaple for a rate bar ,which is used to rate cetain events or programs,here user can rate
the event ,,example program is given below,,








steps 1:


Eclipse- new android project-givename for project-select the target-give package name-then finish







source code


jithuActivity.java


package a.b;
 
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.TextView;
import android.widget.Toast;
 
public class jithuActivity extends Activity {
 
  private RatingBar ratingBar;
  private TextView txtRatingValue;
  private Button btnSubmit;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
addListenerOnRatingBar();
addListenerOnButton();
 
  }
 
  public void addListenerOnRatingBar() {
 
ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);
 
//if rating value is changed,
//display the current rating value in the result (textview) automatically
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
 
txtRatingValue.setText(String.valueOf(rating));
 
}
});
  }
 
  public void addListenerOnButton() {
 
ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
btnSubmit = (Button) findViewById(R.id.button1);
 
//if click on me, then display the current rating value.
btnSubmit.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
 
Toast.makeText(jithuActivity.this,
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_SHORT).show();
 
}
 
});
 
  }
}


main.xml



<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_width="wrap_content" 
        android:text="your rating is : " 
        android:id="@+id/lblResult" 
        android:layout_x="23dp" 
        android:layout_y="281dp">
        </TextView>
        <RatingBar 
        android:stepSize="1.0" 
        android:id="@+id/ratingBar1" 
        android:numStars="3" 
        android:layout_height="wrap_content" 
        android:rating="1" 
        android:layout_width="wrap_content" 
        android:layout_x="29dp" 
        android:layout_y="173dp">
        </RatingBar>
        <Button 
        android:layout_height="wrap_content"
         android:layout_width="wrap_content" 
         android:text="submit" 
         android:id="@+id/button1" 
         android:layout_x="228dp" 
         android:layout_y="174dp">
         </Button>
        <TextView 
        android:layout_height="34dp" 
        android:layout_width="fill_parent" 
        android:text="rate the film" 
        android:layout_x="2dp" 
        android:layout_y="109dp">
        </TextView>
        <TextView 
        android:layout_height="44dp" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:layout_width="54dp" 
        android:text="" android:id="@+id/txtRatingValue" 
        android:layout_x="178dp"
         android:layout_y="284dp">
         </TextView>
 
   
</AbsoluteLayout>







output 










No comments:

Post a Comment