Ofir Ofir - 1 year ago 53
Android Question

How to create YouTube-like voting chart on android?

On YouTube, there's a line divided to green and red based on likes and dislikes. I would like to implement something similar in my app, and maybe even an animation for the colors/lines entrance. I currently have a JSON file with the number of upvotes and downvotes. How would you create it?

Answer Source

Using ProgressBar that is already available to us, we can create a progress bar that displays the number of up votes vs. down votes like so:

your_activity.xml , you'll have to position it where you want to

    android:layout_height="wrap_content" />


ProgressBar progressBar = (ProgressBar) findViewById(R.id.prgrsbar);

int upVote = 300; //JSON upvote data goes here
int downVote = 100; //JSON downvote data goes here

int total = upVote + downVote;
float upVotePercentage = (float)upVote / total;
int upVoteRounded = Math.round(upVotePercentage * 100);