3

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Can you provide an example string that you are parsing? – Submersed Jun 27 '17 at 15:00
  • Submersed, it does not compile. I have replaced the str.substring() with "FF" and it's the same – Alex Ioja-Yang Jun 27 '17 at 15:03
  • the only `Color.valueOf(?)` Method i could find is the one in JavaFX. In case it is, just use `Color.valueOf(someHexString)` and it should work fine – Tom K Jun 27 '17 at 15:10
  • @TomK https://developer.android.com/reference/android/graphics/Color.html#valueOf(float,%20float,%20float) – Tom Sep 26 '17 at 23:52

2 Answers2

8

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.

Submersed
  • 8,810
  • 2
  • 30
  • 38
  • Not sure why but this way I get the following error: Error:(20, 32) error: incompatible types: int cannot be converted to Color. I am not even using any int... – Alex Ioja-Yang Jun 27 '17 at 15:06
  • 1
    Color.rgb returns an int. An int is a Color, the Color class is just a convenience class for manipulating colors. – Submersed Jun 27 '17 at 15:08
  • Incompatible types: Required android.graphics.Color ; Found int – Alex Ioja-Yang Jun 27 '17 at 15:09
  • Make your method return an int, and just return Color.rgb(...); All methods on Color are static, so you don't really need a object of type Color. You just need the int representation of the Color. – Submersed Jun 27 '17 at 15:09
  • So methods can't return Color basically. I have to make the conversion when I need to use the Color, right? – Alex Ioja-Yang Jun 27 '17 at 15:13
  • Yep, methods don't return Color just int. Any API that takes a color will be expecting it in int form, the Color class is just helpful for making those ints. – Submersed Jun 27 '17 at 15:17
2

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)

Star_Man
  • 1,091
  • 1
  • 13
  • 30