Dec 16, 2010

Interaction with RatingBar

Interaction with RatingBar

Override RatingBar.OnRatingBarChangeListener() to interact with RatingBar, assign rating via Rating.setRating() method.

The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) do not support user interaction and should only be used as indicators.


workspace/AndroidRating/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RatingBar
android:id="@+id/ratingbar1"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
/>
<RatingBar
android:id="@+id/ratingbar2"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleIndicator"
/>
<RatingBar
android:id="@+id/ratingbar3"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyle"
/>
</LinearLayout>


workspace/AndroidRating/src/com/AndroidRating/AndroidRating.java
package com.AndroidRating;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

public class AndroidRating extends Activity {

final float MaxRating = 5.0f;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RatingBar ratingBar1 = (RatingBar)findViewById(R.id.ratingbar1);
final RatingBar ratingBar2 = (RatingBar)findViewById(R.id.ratingbar2);
RatingBar ratingBar3 = (RatingBar)findViewById(R.id.ratingbar3);

ratingBar3.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
// TODO Auto-generated method stub
ratingBar1.setRating(arg1);
ratingBar2.setRating(MaxRating - arg1);
}
});
}
}




No comments:

Post a Comment

Infolinks In Text Ads