Android Ratingbar Cannot render incorrectly above Lollipop in ViewPager

149 Views Asked by At

I made ratingbar inside a viewpager. When I slided the page, it's not problem, but when I not slide it, the ratingbar isnot showing and become black and blue. then when i let the app stay on like that in a minute, the app closed itself.

when i slide it

when i am not slide it

this is the code:

 final int y =i;
                nama[y] = new TextView(context);
                nama[y].setLayoutParams(lhoriparams);
                nama[y].setText(arrkonten.get(i)[0]);


                rating[y] = new AppCompatRatingBar(context, null, android.R.attr.ratingBarStyleIndicator);
                rating[y].setLayoutParams(ratingparams);

                float f = Float.parseFloat(arrkonten.get(i)[1]);
                rating[y].setRating(f);

            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                rating[y].setBackgroundTintList(ColorStateList.valueOf(Color.TRANSPARENT));
                rating[y].setSecondaryProgressTintList(ColorStateList.valueOf(Color.TRANSPARENT));
                rating[y].setProgressTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.blue)));
                rating[y].setElevation(10);
            }

            //rating[y].setStepSize(1/10);
                rating[y].setNumStars(5);
                rating[y].setIsIndicator(true);


                linfo[i].addView(nama[y]);
                linfo[i].addView(rating[y]);

please help me, Thx.

2

There are 2 best solutions below

0
On

The last time I tried putting a RatingBar in my app, I had a lot of issues as well, I think it's device-related, on my device I would make it look ok, but then on some device if would appear similar to your screenshot.

I ended up creating a RatingBar on my own, it's quite easy actually, you create 2 pngs one for a selected star, and one for a not-selected star. Then create something similar in a layout xml, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:minWidth="100dp"
              android:orientation="horizontal">
    <ImageButton
        android:id="@+id/one"
        style="@style/RatingBar.Star"/>
    <ImageButton
        android:id="@+id/two"
        style="@style/RatingBar.Star"/>
    <ImageButton
        android:id="@+id/three"
        style="@style/RatingBar.Star"/>
    <ImageButton
        android:id="@+id/four"
        style="@style/RatingBar.Star"/>
    <ImageButton
        android:id="@+id/five"
        style="@style/RatingBar.Star"/>
</LinearLayout>

Where the style is:

<style name="RatingBar.Star" parent="EmptyParent">
    <item name="android:layout_width">30dp</item>
    <item name="android:layout_height">30dp</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:background">@drawable/star_not_selected</item>
    <item name="android:scaleType">fitCenter</item>
    <item name="android:onClick">onStarClicked</item>
</style>

Then you implement in your activity a method public void onStarClicked(View v) to catch clicks on stars on modify the background image from 0 to the clicked star.

0
On

I dont know what's wrong with the default RatingBar, but I have solved it, try use this library https://github.com/ome450901/SimpleRatingBar