Android Open Source - AndroidPlaces Simple Rating Bar






From Project

Back to project page AndroidPlaces.

License

The source code is released under:

Apache License

If you think the Android project AndroidPlaces listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Brian Lee/*from  w  w w .  j a v  a 2  s . c  o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tigerpenguin.widget.simpleratingbar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Optional XML attributes:
 * -SRB_hideRatingNumber: boolean. When set to true, hides the rating number value. Default is false.
 * -SRB_size: dimension. Size of rating number text and icons.
 * -SRB_maxRating: int. Maximum possible rating. Default is DEFAULT_MAX_RATING.
 * -SRB_filledIcon: String. Single character to use for filled icon. Default is filled star. Refer to rating_icons.xml
 * -SRB_unfilledIcon: String. Single character to use for unfilled icon. Default is same as filledIcon.
 * -SRB_textColor: color. Color of rating number text. Default is DEFAULT_FILLED_COLOR_ID.
 * -SRB_filledIconColor: color. Color of filled rating icon. Default is DEFAULT_FILLED_COLOR_ID.
 * -SRB_unfilledIconColor: color. Color of unfilled rating icon. Default is DEFAULT_UNFILLED_COLOR_ID.
 */
public class SimpleRatingBar extends LinearLayout {

    static final int DEFAULT_MAX_RATING = 5;
    static final int DEFAULT_FILLED_COLOR_ID = android.R.color.holo_orange_dark;
    static final int DEFAULT_UNFILLED_COLOR_ID = android.R.color.darker_gray;

    private String[] filledIcons;
    private String[] unfilledIcons;

    private int maxRating;
    private boolean hideRatingNumber;
    private TextView ratingNumber;
    private TextView filledRating;
    private TextView unfilledRating;

    public SimpleRatingBar(Context context) {
        super(context);
        init(context, null);
    }

    public SimpleRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SimpleRatingBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    /**
     * Sets the rating.
     * If rating is less than 0 or greater than maxRating, the number will be displayed as is.
     * However, the icons themselves will be between 0 ~ maxRating icons.
     * @param rating rating.
     */
    public void setRating(int rating) {
        if (!hideRatingNumber) {
            ratingNumber.setText(String.valueOf(rating));
        }
        setIcons(rating);
    }

    /**
     * Sets the rating.
     * If rating is less than 0 or greater than maxRating, the number will be displayed as is.
     * However, the icons themselves will be between 0 ~ maxRating icons.
     * The icons will not show half-icons, but will round up/down to the closest integer to show the icons.
     * @param rating rating.
     */
    public void setRating(double rating) {
        if (!hideRatingNumber) {
            ratingNumber.setText(String.valueOf(rating));
        }
        int roundedRating = (int) Math.round(rating);
        setIcons(roundedRating);
    }

    private void setIcons(int rating) {
        if (rating > maxRating) {
            rating = maxRating;
        } else if (rating < 0) {
            rating = 0;
        }

        if (rating == 0) {
            filledRating.setText("");
            unfilledRating.setText(unfilledIcons[maxRating - 1]);
        } else if (rating == maxRating) {
            filledRating.setText(filledIcons[maxRating - 1]);
            unfilledRating.setText("");
        } else {
            filledRating.setText(filledIcons[rating - 1]);
            unfilledRating.setText(unfilledIcons[maxRating - rating - 1]);
        }
    }

    private void init(Context context, AttributeSet attributeSet) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.simple_rating_bar, this);
        ratingNumber = (TextView) findViewById(R.id.ratingNumber);
        filledRating = (TextView) findViewById(R.id.filledRating);
        unfilledRating = (TextView) findViewById(R.id.unfilledRating);

        SimpleRatingBarAttributes attributes = new SimpleRatingBarAttributes(context, attributeSet);
        readAttributes(attributes);
    }

    private void readAttributes(SimpleRatingBarAttributes attributes) {
        hideRatingNumber = attributes.getHideRatingNumber();
        maxRating = attributes.getMaxRating();
        if (maxRating <= 0) {
            maxRating = DEFAULT_MAX_RATING;
        }

        filledIcons = new String[maxRating];
        String filledIcon = attributes.getFilledIcon();
        if (filledIcon == null || filledIcon.length() != 1) {
            filledIcon = getContext().getString(R.string.ratingIconStarFilled);
        }
        setIcons(filledIcons, filledIcon);

        String unfilledIcon = attributes.getUnfilledIcon();
        if (unfilledIcon == null || unfilledIcon.length() != 1 || filledIcon.equals(unfilledIcon)) {
            unfilledIcons = filledIcons;
        } else {
            unfilledIcons = new String[maxRating];
            setIcons(unfilledIcons, unfilledIcon);
        }

        if (attributes.getSize() > 0) {
            ratingNumber.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getSize());
            filledRating.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getSize());
            unfilledRating.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getSize());
        }

        ratingNumber.setTextColor(attributes.getTextColor());
        filledRating.setTextColor(attributes.getFilledIconColor());
        unfilledRating.setTextColor(attributes.getUnfilledIconColor());

        if (hideRatingNumber) {
            ratingNumber.setVisibility(View.GONE);
        } else {
            ratingNumber.setVisibility(View.VISIBLE);
        }
    }

    private void setIcons(String[] icons, String icon) {
        StringBuilder iconBuilder = new StringBuilder();
        for (int i = 0; i < icons.length; i++) {
            iconBuilder.append(icon);
            icons[i] = iconBuilder.toString();
        }
    }

    // exposed for testing
    static class SimpleRatingBarTester {

        private final SimpleRatingBar simpleRatingBar;

        SimpleRatingBarTester(SimpleRatingBar simpleRatingBar) {
            this.simpleRatingBar = simpleRatingBar;
        }

        void readAttributes(SimpleRatingBarAttributes attributes) {
            simpleRatingBar.readAttributes(attributes);
        }

        CharSequence getRatingTextDisplayed() {
            return simpleRatingBar.ratingNumber.getText();
        }

        CharSequence getFilledRatingDisplayed() {
            return simpleRatingBar.filledRating.getText();
        }

        CharSequence getUnfilledRatingDisplayed() {
            return simpleRatingBar.unfilledRating.getText();
        }

        float getRatingNumberTextSize() {
            return simpleRatingBar.ratingNumber.getTextSize();
        }

        float getFilledIconTextSize() {
            return simpleRatingBar.filledRating.getTextSize();
        }

        float getUnfilledIconTextSize() {
            return simpleRatingBar.unfilledRating.getTextSize();
        }

        int getRatingNumberVisibility() {
            return simpleRatingBar.ratingNumber.getVisibility();
        }

        int getRatingColor() {
            return simpleRatingBar.ratingNumber.getCurrentTextColor();
        }

        int getFilledIconColor() {
            return simpleRatingBar.filledRating.getCurrentTextColor();
        }

        int getUnfilledIconColor() {
            return simpleRatingBar.unfilledRating.getCurrentTextColor();
        }
    }
}




Java Source Code List

com.tigerpenguin.demo.places.PlaceDetailActivity.java
com.tigerpenguin.demo.places.PlaceResultAdapter.java
com.tigerpenguin.demo.places.PlaceResultsFragment.java
com.tigerpenguin.demo.places.PlaceReviewAdapter.java
com.tigerpenguin.demo.places.PlaceReviewsFragment.java
com.tigerpenguin.demo.places.PlaceThumbnailAdapter.java
com.tigerpenguin.demo.places.PlaceThumbnailsFragment.java
com.tigerpenguin.demo.places.PlacesDemo.java
com.tigerpenguin.places.PlacesTest.java
com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java
com.tigerpenguin.places.deserializer.PlaceTypeDeserializer.java
com.tigerpenguin.places.model.AddressComponent.java
com.tigerpenguin.places.model.AspectRating.java
com.tigerpenguin.places.model.AspectType.java
com.tigerpenguin.places.model.DayOfWeek.java
com.tigerpenguin.places.model.DayTime.java
com.tigerpenguin.places.model.Geometry.java
com.tigerpenguin.places.model.JsonModel.java
com.tigerpenguin.places.model.Language.java
com.tigerpenguin.places.model.OpeningHours.java
com.tigerpenguin.places.model.Period.java
com.tigerpenguin.places.model.Photo.java
com.tigerpenguin.places.model.PlaceDetail.java
com.tigerpenguin.places.model.PlaceLocation.java
com.tigerpenguin.places.model.PlaceType.java
com.tigerpenguin.places.model.Place.java
com.tigerpenguin.places.model.PriceLevel.java
com.tigerpenguin.places.model.RankBy.java
com.tigerpenguin.places.model.Review.java
com.tigerpenguin.places.model.StatusCode.java
com.tigerpenguin.places.request.InvalidRequestException.java
com.tigerpenguin.places.request.NearbySearchRequestTest.java
com.tigerpenguin.places.request.NearbySearchRequest.java
com.tigerpenguin.places.request.PlaceDetailRequestTest.java
com.tigerpenguin.places.request.PlaceDetailRequest.java
com.tigerpenguin.places.request.PlaceSearchRequestTest.java
com.tigerpenguin.places.request.PlaceSearchRequest.java
com.tigerpenguin.places.request.PlacesRequestTest.java
com.tigerpenguin.places.request.PlacesRequest.java
com.tigerpenguin.places.request.RequestTest.java
com.tigerpenguin.places.response.NearbySearchResponseFactory.java
com.tigerpenguin.places.response.NearbySearchResponse.java
com.tigerpenguin.places.response.PlaceDetailResponseFactory.java
com.tigerpenguin.places.response.PlaceDetailResponse.java
com.tigerpenguin.places.response.PlaceSearchResponse.java
com.tigerpenguin.places.response.PlacesResponseFactory.java
com.tigerpenguin.places.response.PlacesResponse.java
com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlaceDetailResponseHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlacesResponseHandler.java
com.tigerpenguin.widget.simpleratingbar.MockAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarTest.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar.java