Skip to content

Instantly share code, notes, and snippets.

@griajobag
Created May 25, 2018 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griajobag/ba803dd5e8050fa27dd787160c25bd21 to your computer and use it in GitHub Desktop.
Save griajobag/ba803dd5e8050fa27dd787160c25bd21 to your computer and use it in GitHub Desktop.
Rating
/**
* This method used to display rating by colors
*
* @param productModel
*/
private void setRatingByColor(ProductModel productModel) {
int widthView = constrainLayout1.getWidth();
int totalAllVoters = productModel.getTotalVoters();
int totalRateStar1 = productModel.getStar1();
int totalRateStar2 = productModel.getStar2();
int totalRateStar3 = productModel.getStar3();
int totalRateStar4 = productModel.getStar4();
int totalRateStar5 = productModel.getStar5();
//convert to double
double votersInDouble = (double) totalAllVoters;
//RATING STAR 1
double star1 = (double) totalRateStar1;
double sum1 = (star1 / votersInDouble);
int rating1 = (int) (sum1 * widthView);
ConstraintLayout.LayoutParams layoutParams1 = new ConstraintLayout.LayoutParams(rating1, ConstraintLayout.LayoutParams.MATCH_PARENT);
layoutParams1.setMargins(0, 5, 0, 5);
llPercentage1.setBackgroundColor(Color.parseColor("#ff6f31"));
llPercentage1.setLayoutParams(layoutParams1);
//RATING STAR 2
double star2 = (double) totalRateStar2;
double sum2 = (star2 / votersInDouble);
int rating2 = (int) (sum2 * widthView);
ConstraintLayout.LayoutParams layoutParams2 = new ConstraintLayout.LayoutParams(rating2, ConstraintLayout.LayoutParams.MATCH_PARENT);
layoutParams2.setMargins(0, 5, 0, 5);
llPercentage2.setBackgroundColor(Color.parseColor("#ff9f02"));
llPercentage2.setLayoutParams(layoutParams2);
//RATING STAR 3
double star3 = (double) totalRateStar3;
double sum3 = (star3 / votersInDouble);
int rating3 = (int) (sum3 * widthView);
ConstraintLayout.LayoutParams layoutParams3 = new ConstraintLayout.LayoutParams(rating3, ConstraintLayout.LayoutParams.MATCH_PARENT);
layoutParams3.setMargins(0, 5, 0, 5);
llPercentage3.setBackgroundColor(Color.parseColor("#ffcf02"));
llPercentage3.setLayoutParams(layoutParams3);
//RATING STAR 4
double star4 = (double) totalRateStar4;
double sum4 = (star4 / votersInDouble);
int rating4 = (int) (sum4 * widthView);
ConstraintLayout.LayoutParams layoutParams4 = new ConstraintLayout.LayoutParams(rating4, ConstraintLayout.LayoutParams.MATCH_PARENT);
layoutParams4.setMargins(0, 5, 0, 5);
llPercentage4.setBackgroundColor(Color.parseColor("#9ace6a"));
llPercentage4.setLayoutParams(layoutParams4);
//RATING STAR 5
double star5 = (double) totalRateStar5;
double sum5 = (star5 / votersInDouble);
int rating5 = (int) (sum5 * widthView);
ConstraintLayout.LayoutParams layoutParams5 = new ConstraintLayout.LayoutParams(rating5, ConstraintLayout.LayoutParams.MATCH_PARENT);
layoutParams5.setMargins(0, 5, 0, 5);
llPercentage5.setBackgroundColor(Color.parseColor("#57bb8a"));
llPercentage5.setLayoutParams(layoutParams5);
// menampilkan rating berdasarkan angka
int totalBintangSatu = totalRateStar1 * 1;
int totalBintangDua = totalRateStar2 * 2;
int totalBintangTiga = totalRateStar3 * 3;
int totalBintangEmpat = totalRateStar4 * 5;
int totalBintangLima = totalRateStar5 * 5;
double sumBintang = totalBintangSatu +
totalBintangDua +
totalBintangTiga +
totalBintangEmpat +
totalBintangLima;
double rating = (sumBintang / votersInDouble);
DecimalFormat format = new DecimalFormat(".#");
tvTotalNumberRating.setText(String.valueOf(format.format(rating)));
totalStarRating.setRating(Float.parseFloat(String.valueOf(rating)));
tvTotalPemberiBintang.setText(String.valueOf(totalAllVoters) + " total");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment