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.SeekBar; 23 import android.widget.TextView; 24 25 import com.example.android.apis.R; 26 27 /** 28 * Demonstrates how to use a rating bar 29 */ 30 public class RatingBar1 extends Activity implements RatingBar.OnRatingBarChangeListener { 31 RatingBar mSmallRatingBar; 32 RatingBar mIndicatorRatingBar; 33 TextView mRatingText; 34 35 @Override onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 39 setContentView(R.layout.ratingbar_1); 40 41 mRatingText = (TextView) findViewById(R.id.rating); 42 43 // We copy the most recently changed rating on to these indicator-only 44 // rating bars 45 mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar); 46 mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar); 47 48 // The different rating bars in the layout. Assign the listener to us. 49 ((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this); 50 ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this); 51 } 52 onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch)53 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) { 54 final int numStars = ratingBar.getNumStars(); 55 mRatingText.setText( 56 getString(R.string.ratingbar_rating) + " " + rating + "/" + numStars); 57 58 // Since this rating bar is updated to reflect any of the other rating 59 // bars, we should update it to the current values. 60 if (mIndicatorRatingBar.getNumStars() != numStars) { 61 mIndicatorRatingBar.setNumStars(numStars); 62 mSmallRatingBar.setNumStars(numStars); 63 } 64 if (mIndicatorRatingBar.getRating() != rating) { 65 mIndicatorRatingBar.setRating(rating); 66 mSmallRatingBar.setRating(rating); 67 } 68 final float ratingBarStepSize = ratingBar.getStepSize(); 69 if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) { 70 mIndicatorRatingBar.setStepSize(ratingBarStepSize); 71 mSmallRatingBar.setStepSize(ratingBarStepSize); 72 } 73 } 74 75 } 76