Amine Choukri
Amine Choukri

Reputation: 408

How to get a color depends on the percentage calculated

I want to create a component with a color in it background , this color is variable from green to red ... well I have a courses class with limit places for example 10 places = 100%, students can join this class so for each student i need to calculate the percentage and using this percentage I change the color of the component until 100% and here the component will have Red color that's mean there is no other place

Upvotes: 0

Views: 220

Answers (1)

Naveen Dew
Naveen Dew

Reputation: 1295

You need something like this

private int getcolorPercentage(float percent) {

        return (Integer) new ArgbEvaluator().evaluate(percent, 0xff00ff00, 0xffff0000);
    }

Usage

int percent = 10;

view.setBackgroundColor(getcolorPercentage((float)percent/10‌​0));

Reference:

android/animation/ArgbEvaluator

Upvotes: 1

Related Questions