Material Design RatingBar with better appearance.
RatingBar with better appearance, compatible with Android 3.0+.
layout_widthis set to
match_parent, as in Google Play Store.
layout_heightis set to values other than 16dp, 36dp and 48dp.
4.3correctly, which will be filled to
4.5by framework's incorrect implementation.
Simply replace your
MaterialRatingBar, and remember to apply a corresponding style for correct behavior.
For example, to create a normal
<me.zhanghai.android.materialratingbar.MaterialRatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/Widget.MaterialRatingBar.RatingBar" />
In order to make your
RatingBar take the correct and consistent size on all versions, you will always need to use one of the styles from this library. The trick inside it is
android:maxHeight that controls the drawable height.
You can checkout more small or indicator variants in styles.xml
8 tint-related attributes such as
app:mrb_progressTintMode are also supported so that they can control the tinting of rating drawables. The default tint color is
?colorControlActivated, and the default tint mode is
OnRatingChangeListener interface is also added to
MaterialRatingBar, which enables callback while user is dragging, just as the listener in
For a detailed example, you can refer to the sample app's layout, where you can find examples such as tinting and wide layout.
RatingBar uses filling stars with grey color as track, however as per the Material Icons site, star border icons are given.
And as for the Google Play Store and Google I/O app, they are both using the star borders as track.
With Google's design practice and aesthetic considerations taken into account, I decided to use the star border style.
Google Play Store has stars of optical size 24dp, while Google I/O app and framework
Widget.Material.RatingBar.Indicator have stars of size 36dp (which are of optical size 30dp). (The framework's default size of 64dp is ridiculously large and thus not taken into consideration.)
Also noticing that the Material Icons site gives icons of 24dp (optical 20dp) and 36dp (optical 30dp), I decided to stick to the 36dp approach which is also visually pleasant.
The ring for radio button in Material Design has a width of 2dp, and with experiments on other border widths, I decided to adopt the 2dp border width.
The star border icon is drawn with the help of Inkscape, by downloading the star icon SVG from Material Icons, duplicating the outer border path of the star, setting a stroke of 4dp, running stroke to path on it, extracting the inner border path, and finally combining this path and the original outer border path.
RatingBar gives erroneous rendering for
layout_width is set to
match_parent by tiling the stars without any gap. Since Google Play Store employed the wide design, I implemented it inside this library as well, so that
match_parent will work properly for
Google Play Store and Google I/O app both used an implementation other than
RatingBar, which means dragging on the bar across stars won't work (it is the functionality of
SeekBar). I think this is a handy way of interaction for users, and it enables the setting of 0 star which can be useful if you want to enable users to reset their rating to unrated.
Опубликовать в Twitter Опубликовать в Facebook
Copyright 2016 Zhang Hai 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.