how to set an int color in a Text composable like ...
# compose
p
how to set an int color in a Text composable like we did in views? This was the old method for setting the int color to a textview:
Copy code
/**
 * Sets the text color for all the states (normal, selected,
 * focused) to be this color.
 *
 * @param color A color value in the form 0xAARRGGBB.
 * Do not pass a resource ID. To get a color value from a resource ID, call
 * {@link androidx.core.content.ContextCompat#getColor(Context, int) getColor}.
 *
 * @see #setTextColor(ColorStateList)
 * @see #getTextColors()
 *
 * @attr ref android.R.styleable#TextView_textColor
 */
@android.view.RemotableViewMethod
public void setTextColor(@ColorInt int color) {
    mTextColor = ColorStateList.valueOf(color);
    updateTextColors();
}
s
Try this:
Text(..., color = colorResource(R.color.abcd))
p
is not a resource
s
What do you have at hand right now for the color you mention?
p
I did this previously
Copy code
tv.setTextColor(getColor());
s
What is
getColor()
p
returns a color represented by an int
check the function
Copy code
public void setTextColor(@ColorInt int color) {
    mTextColor = ColorStateList.valueOf(color);
    updateTextColors();
}
you can have colors like this
Copy code
color1=Color.argb(a, r, g, b);
Copy code
@ColorInt
public static int argb(
        @IntRange(from = 0, to = 255) int alpha,
        @IntRange(from = 0, to = 255) int red,
        @IntRange(from = 0, to = 255) int green,
        @IntRange(from = 0, to = 255) int blue) {
    return (alpha << 24) | (red << 16) | (green << 8) | blue;
}
those are functions from android, not mine
y
use this Color(0xFFDA30F5).toArgb()
p
why not simply this?
Copy code
color = Color(0x00000000)
or
Copy code
color = Color(0)
it seems to compile
maybe is what I was searching for
k
Hey Pablo, setting color is similar to that. We need to use the Color(0XFF______). on the place of _. there should be your color. remember 0XFF is mandatory and after that you need to write the color code.
d
Here is one way:
Copy code
val myGreen = Color(0xff_00_bb_00)

Text(
    text = "Test",
    color = myGreen
)
e
@Khubaib Khan I think it might be good to explain why “OxFF is mandatory”
k
@efemoney right.
292 Views