0

How do I make ratingBar with a border like this: https://materialdoc.com/components/rating-bar/?

My ratingBar xml:

<RatingBar
        android:theme="@style/RatingBar"
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="2"
        android:numStars="5"
        android:stepSize="1" />

App styles:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
<style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
</style>

This is what I get: enter image description here

With color filters:

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_ATOP);

enter image description here

Mikhail
  • 800
  • 8
  • 21

2 Answers2

1

Solution: You can use custom drawable to rating bar.

Try this

XML CODE

 <RatingBar
    android:id="@+id/ratingBar"
    style="@style/MyRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="48dp"
    android:stepSize="0.10"   <!-- Set Stepsize as per your need -->
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText3" />

THEME CODE

  <style name="MyRatingBar" parent="Widget.AppCompat.RatingBar">
    <item name="android:progressDrawable">@drawable/custom_ratingbar</item>
</style>

custom_ratingbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <bitmap
        android:src="@drawable/ic_ic_star_border_blue_24dp"
        android:tint="?attr/colorControlNormal" />
</item>
<item android:id="@android:id/secondaryProgress">
    <bitmap
        android:src="@drawable/ic_ic_star_border_blue_24dp"
        android:tint="?attr/colorControlActivated" />
</item>
<item android:id="@android:id/progress">
    <bitmap
        android:src="@drawable/ic_ic_star_blue_24dp"
        android:tint="?attr/colorControlActivated" />
</item>

Output:

enter image description here

enter image description here

UPDATED

Here is the source for the material icons https://material.io/icons/.

Use this source link to apply colour for your drawable https://romannurik.github.io/

Hope it helps..!

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
0

Use below code :

References :https://stackoverflow.com/a/37135661/8448886

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
    stars.getDrawable(0).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
    stars.getDrawable(1).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44