setColor not working after Android 10 (API 29) upgrade

1

My code

paint.setColor(0xFFFFFFFF);

was successfully running before upgrading to Android 10. Now it throws an error:

Wrapped java.lang.IllegalArgumentException: Invalid ID, must be in the range [0..16)

(Looks like the error message is trimmed)

Nevertheless,

paint.setARGB(255,255,255,255);

works fine.

I've read that starting API 29, setColor also accepts long color values, I tried explicitly putting

paint.setColor(parseInt(0xFFFFFFFF));

or

paint.setColor(valueOf(0xFFFFFFFF));

but none of them worked...

Any solutions in how to use setColor? ARGB doesn't always work for me (e.g. cannot create gradient by specifying ARGB).

java
android
paint
asked on Stack Overflow Dec 22, 2019 by Summer Son • edited Dec 23, 2019 by Izhan Ali

3 Answers

0

You can fix this with a cast:

paint.setColor((int)0xFFFFFFFF);

...although I'm unable to reproduce this behavior on my own machine... 0xFFFFFFFF is always interpreted as an int (even though it's out of range, strictly speaking). Not sure why your compiler is choosing the setColor(long) override.

answered on Stack Overflow Dec 22, 2019 by greeble31
0

Maybe you should try this.

        paint.setColor(ContextCompat.getColor(YourActivity.this,R.color.colorPrimary));
answered on Stack Overflow Dec 23, 2019 by Manish Stark
0

Parameter value is treated as long number so it cant calculate the color space. There are two different functions, valueOf(int) and valueOf(long). Just cast to int to get working with standard int value.

valueOf(0xFFFFFFFF.toInt())
answered on Stack Overflow Jun 11, 2020 by Prcela

User contributions licensed under CC BY-SA 3.0