Reputation: 33
This is my code to convert a String to Color:
public Color prepareColour(String str) {
str.replace("#", "");
float r = Float.valueOf(str.substring(0,1));
float g = Float.valueOf(str.substring(2,3));
float b = Float.valueOf(str.substring(4,5));
Color color = Color.valueOf(r,g,b);
return color;
}
I get the following debug error:
Error:(16, 23) error: constructor Color in class Color cannot be applied to given types; required: no arguments found: float,float,float reason: actual and formal argument lists differ in length
However, the suggestion before compiling from Android Studio is:
Call requires API level 26 (current min is 17) ......
I see there are answers from 2011 supporting this way of creating a Color, so surely it works on API 17 and doesn't require 26.
I have tried cleaning and rebuilding the project, as well as replacing the str.substring
with actual values and nothing changes.
Why will the code not compile?
Upvotes: 3
Views: 10116
Reputation: 8870
Have you tried using Color.rgb(r,g,b)
instead of Color.valueOf(...)
? Color.valueOf(...)
is a very new method in the Android Developer O preview, so it will only be useful on 1 API level at the moment.
Also, make sure you're using ints in the range of 0-255, or floats in the range of 0-1.
Upvotes: 9
Reputation: 1091
You can use this code.
Color.parseColor(String strColor);
This is a static method of Color
class
public static int parseColor (String colorString)
https://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)
Upvotes: 2