Ofir Ofir - 3 months ago 15
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

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

<ProgressBar
    android:indeterminate="false"
    android:id="@+id/prgrsbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:progress="0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

YourActivity.java

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);

progressBar.setProgress(upVoteRounded)
Comments