• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.widget.RatingBar;
22 import android.widget.TextView;
23 
24 import com.example.android.apis.R;
25 
26 /**
27  * Demonstrates how to use a rating bar
28  */
29 public class RatingBar1 extends Activity implements RatingBar.OnRatingBarChangeListener {
30     RatingBar mSmallRatingBar;
31     RatingBar mIndicatorRatingBar;
32     TextView mRatingText;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37 
38         setContentView(R.layout.ratingbar_1);
39 
40         mRatingText = (TextView) findViewById(R.id.rating);
41 
42         // We copy the most recently changed rating on to these indicator-only
43         // rating bars
44         mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar);
45         mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar);
46 
47         // The different rating bars in the layout. Assign the listener to us.
48         ((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
49         ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
50     }
51 
onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch)52     public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
53         final int numStars = ratingBar.getNumStars();
54         mRatingText.setText(
55                 getString(R.string.ratingbar_rating) + " " + rating + "/" + numStars);
56 
57         // Since this rating bar is updated to reflect any of the other rating
58         // bars, we should update it to the current values.
59         if (mIndicatorRatingBar.getNumStars() != numStars) {
60             mIndicatorRatingBar.setNumStars(numStars);
61             mSmallRatingBar.setNumStars(numStars);
62         }
63         if (mIndicatorRatingBar.getRating() != rating) {
64             mIndicatorRatingBar.setRating(rating);
65             mSmallRatingBar.setRating(rating);
66         }
67         final float ratingBarStepSize = ratingBar.getStepSize();
68         if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) {
69             mIndicatorRatingBar.setStepSize(ratingBarStepSize);
70             mSmallRatingBar.setStepSize(ratingBarStepSize);
71         }
72     }
73 
74 }
75