Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Rating Bar show one star only

Tags:

android

enter image description here.

I create a custom rating bar. I set 5 custom star images, but it shows only one. Instead of a variable number of rating drawables, I see only one, regardless of the number of stars set. Any help will be appreciated.

xml.code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<RatingBar
    android:id="@+id/ratingbar_default"
    style="@style/RatingBar"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginBottom="20dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="276dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="show state up checkbox"
    android:textColor="#CC00CC"
    android:textSize="20dp" />

java code :

Activity RatingBarActivity

@Override
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rating);

            final TextView text = (TextView) findViewById(R.id.textView);

            final RatingBar ratingBar_default = (RatingBar) findViewById(R.id.ratingbar_default);

            ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
                {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
                        {
                            // TODO Auto-generated method stub
                            text.setText("Rating: " +         String.valueOf(rating));
                        }
                });

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@android:id/background"
    android:drawable="@drawable/star_ratingbar_full_empty" />
<item
    android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/star_ratingbar_full_empty" />
<item
    android:id="@android:id/progress"
    android:drawable="@drawable/star_ratingbar_full_filled" />
</layer-list>

res/drawable/star_ratingbar_full_empty.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star2" />

<item android:state_focused="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star2" />

 <item android:state_selected="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star2" />

<item android:drawable="@drawable/star2" />

</selector>

res/drawabstar_ratingbar_full_filled.xml:

<?xml version="1.0" encoding="utf-8"?>

<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star3" />

<item android:state_focused="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star1" />

<item android:state_selected="true"
  android:state_window_focused="true"
  android:drawable="@drawable/star1" />

<item android:drawable="@drawable/star1" />

</selector>

styles.xml file :

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>

<style name="RatingBar" parent="@android:style/Widget.RatingBar">
    <item     name="android:progressDrawable">@drawable/star_rating_bar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

</resources>
like image 492
Rizwan Rasheed Avatar asked May 09 '15 20:05

Rizwan Rasheed


3 Answers

In my case trouble was in SVG drawables in layer-list. When I changed them to PNG it starts work correctly

like image 116
Penzzz Avatar answered Nov 12 '22 20:11

Penzzz


Try to specify an android:numStars attribute for your RatingBar in layout file activity_rating.xml. I guess, the default value is 1 - that's why you see only one star

like image 40
Cool Soft Avatar answered Nov 12 '22 18:11

Cool Soft


Just remove any-dpi drawable of rating bar filled and empty image from drawable. And it will work for you. Because it to show custom rating bar you can give both xml and png files in your layer list. But to show all the stars, you have to put only png icons into drawable folder.

like image 1
Sanjay Kushwah Avatar answered Nov 12 '22 18:11

Sanjay Kushwah