-4

I dynamically create my layout like this. I wanted to change the color of the star in this rating bar from default blue to yellow.

View insertPhoto( int id, int i){
     layout = new LinearLayout(getApplicationContext());
     layout.setLayoutParams(new LayoutParams(180, 250));
     layout.setGravity(Gravity.CENTER);
     layout.setOrientation(1);
     layout.setId(i);
     FrameLayout fl=new FrameLayout(getApplicationContext());
     fl.setLayoutParams(new LayoutParams(116, 116));
     imageView = new ImageView(getApplicationContext());
     imageView.setLayoutParams(new LayoutParams(116,116));
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
     imageView.setImageResource(id);
     imageView.setId(i);
     imgoff = new ImageView(getApplicationContext());
     imgoff.setLayoutParams(new LayoutParams(50,116));
     imgoff.setScaleType(ImageView.ScaleType.FIT_END);
     imgoff.setImageResource(getResources().getIdentifier("offer","drawable", getPackageName()));
    // imgoff.setOnClickListener(listener);
     fl.addView(imageView);
     fl.addView(imgoff);
     tv=new TextView(getApplicationContext());
     tv.setLayoutParams(new LayoutParams(116,20));
     tv.setText(namesArr[i]);
     tv.setTextColor(Color.BLACK);
     rb=new RatingBar(getApplicationContext(),null,android.R.attr.ratingBarStyleSmall);
     rb.setLayoutParams(new LayoutParams(80,50));
     rb.setIsIndicator(true);
     rb.setNumStars(5);
     rb.setRating(rateFArr[i]);
     layout.addView(fl);
     layout.addView(tv);
      layout.addView(rb);
     layout.setOnClickListener(listener);
     return layout;

    }

Please can someone help me fix this. Thanks in advance.

Cԃաԃ
  • 1,259
  • 1
  • 15
  • 29
user2609258
  • 39
  • 2
  • 6

1 Answers1

3

Option 1:

Step #1: Create your own style, by cloning one of the existing styles (from $ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml), putting it in your own project's styles.xml, and referencing it when you add the widget to a layout.

Step #2: Create your own LayerDrawable XML resources for the RatingBar, pointing to appropriate images to use for the bar. The original styles will point you to the existing resources that you can compare with. Then, adjust your style to use your own LayerDrawable resources, rather than built-in ones.

Option 2:

You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.

Put these 3 images at res/drawable.

Put there the following ratingbar_red.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" android:drawable="@drawable/red_star_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/red_star_half" />
    <item android:id="@android:id/progress" android:drawable="@drawable/red_star_full" />
</layer-list>

and, finally, tell your ratingbar definition to use this, i.e.

<RatingBar android:progressDrawable="@drawable/ratingbar_red/>

That's it.

Jay
  • 1,474
  • 9
  • 13
  • rb=new RatingBar(getApplicationContext(),null,R.Style.ratingbar_red); still it did not worked out the rating bar itself disappears. I even checked for layout size all that is fine. – user2609258 Jul 23 '13 at 06:17
  • Hi Jay, I've tried this solution but when I try to set the rating to something like 1.1, 1.2, etc... it's display as 1.5. I have taken 3 star images, one empty, one half full and one full. I presume it's supposed to interpolate between them to get the fractional components, I'm just wondering has anyone else had the same issue or am I doing something noticeably wrong? – Graham Baitson Jan 10 '14 at 11:10
  • that answer helped me a lot! thank you sir! – JozeRi Jul 26 '15 at 12:09