android - how to change the color RatingBar depending on the number of stars? -
how following?
if ratingbar has 1-3 stars - red stars. if ratingbar has 4 stars - yellow stars. if ratingbar has 5 stars - green stars.
((ratingbar) layout.findviewbyid(r.id.ratingbar)).setprogress(integer.parseint(surveybeans.get(section).get(position).getrate())); <ratingbar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ratingbar" android:stepsize="1" android:clickable="false" style="?android:attr/ratingbarstylesmall" android:layout_gravity="right" android:layout_marginright="15dp" />
edit:
@override public view getitemview(int section, int position, view convertview, viewgroup parent) { linearlayout layout; if (convertview == null) { layoutinflater inflator = (layoutinflater) parent.getcontext().getsystemservice(context.layout_inflater_service); layout = (linearlayout) inflator.inflate(r.layout.item_survey, null); } else { layout = (linearlayout) convertview; } ((textview) layout.findviewbyid(r.id.questionsurvey)).settext(surveybeans.get(section).get(position).getquestion()); ratingbar ratingbar = ((ratingbar) layout.findviewbyid(r.id.ratingbar)); ratingbar.setprogress(integer.parseint(surveybeans.get(section).get(position).getrate())); layerdrawable stars = (layerdrawable) ratingbar.getprogressdrawable(); stars.getdrawable(2).setcolorfilter(color.yellow, porterduff.mode.src_atop); stars.getdrawable(1).setcolorfilter(color.red, porterduff.mode.src_atop); ((textview) layout.findviewbyid(r.id.commentsurvey)).settext(surveybeans.get(section).get(position).getcomment()); return layout; }
working code after answering @murtaza hussain:
ratingbar ratingbar = ((ratingbar) layout.findviewbyid(r.id.ratingbar)); ratingbar.setprogress(integer.parseint(surveybeans.get(section).get(position).getrate())); layerdrawable stars = (layerdrawable) ratingbar.getprogressdrawable(); if (ratingbar.getrating()<=3) { stars.getdrawable(2).setcolorfilter(color.red, porterduff.mode.src_atop); } if(ratingbar.getrating()==4){ stars.getdrawable(2).setcolorfilter(color.yellow, porterduff.mode.src_atop); } if(ratingbar.getrating()==5){ stars.getdrawable(2).setcolorfilter(color.green, porterduff.mode.src_atop); }
ratingbar ratingbar = (ratingbar) findviewbyid(r.id.ratingbar); layerdrawable stars = (layerdrawable) ratingbar.getprogressdrawable(); stars.getdrawable(2).setcolorfilter(color.yellow, porterduff.mode.src_atop);
or
layerdrawable stars = (layerdrawable) ratingbar.getprogressdrawable(); stars.getdrawable(2).setcolorfilter(getresources().getcolor(r.color.starfullyselected), porterduff.mode.src_atop); stars.getdrawable(1).setcolorfilter(getresources().getcolor(r.color.starpartiallyselected), porterduff.mode.src_atop); stars.getdrawable(0).setcolorfilter(getresources().getcolor(r.color.starnotselected), porterduff.mode.src_atop);
you can on
ratingbar.setonratingbarchangelistener(new onratingbarchangelistener(){ @override public void onratingchanged(ratingbar ratingbar, float rating, boolean fromuser) { // event fired when rating changed } });
from code
@override public view getitemview(int section, int position, view convertview, viewgroup parent) { linearlayout layout; if (convertview == null) { layoutinflater inflator = (layoutinflater) parent.getcontext().getsystemservice(context.layout_inflater_service); layout = (linearlayout) inflator.inflate(r.layout.item_survey, null); } else { layout = (linearlayout) convertview; } ((textview) layout.findviewbyid(r.id.questionsurvey)).settext(surveybeans.get(section).get(position).getquestion()); ratingbar ratingbar = ((ratingbar) layout.findviewbyid(r.id.ratingbar)); ratingbar.setprogress(integer.parseint(surveybeans.get(section).get(position).getrate())); if (ratingbar.getrating() == 2f) { //change color of 2 stars } else if (ratingbar.getrating() == 3f) { //change color of 3 stars } layerdrawable stars = (layerdrawable) ratingbar.getprogressdrawable(); stars.getdrawable(2).setcolorfilter(color.yellow, porterduff.mode.src_atop); stars.getdrawable(1).setcolorfilter(color.red, porterduff.mode.src_atop); ((textview) layout.findviewbyid(r.id.commentsurvey)).settext(surveybeans.get(section).get(position).getcomment()); return layout; }
Comments
Post a Comment