1

Is it possible to reference colors from xml using databinding for android?

This works fine:

android:textColor="@{inputValue == null ? 0xFFFBC02D : 0xFFFFEB3B}"

but this does not:

android:textColor="@{inputValue == null ? @color/red : @color/blue}"

Reported it here: https://issuetracker.google.com/issues/38021292

* EDIT *

It turns out it was just and id issue / bug which pops up in edge cases only. My xml:

    <TextView
        android:id="@+id/input_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{inputValue}"
        android:textColor="@{showAsEmpty ? @color/registerInputEmpty : @color/registerInputSet}"
        tools:text="Select to edit"/>

The problem with it is the inputValue parameter, and the input_value id. They become the same inputValue field in data binding. This is only a problem if you set a custom color. An error from Google, should be fixded in the next update.

Frank
  • 12,010
  • 8
  • 61
  • 78
  • Something is wrong. I'm guessing that you have not defined a `red` or `blue` color resource. It should work with `android:textColor="@color/red"` or `android:textColor="@color/blue"` so please check that. Finally, what is the error. – George Mount May 03 '17 at 14:40

3 Answers3

2

Yes, you can reference colors:

<TextView android:textColor="@{inputValue == null ? @color/red : @color/blue}" .../>

Color resources are loaded as integers. If your attribute expects a Drawable and receives an integer, it is converted to a ColorDrawable. For example:

<View android:background="@{hasError ? @color/errorBg : @color/normalBg}" .../>

If you must refer to the resource ID, use the R value and remember to import R. It is uncommon, but some setters take resource IDs rather than values. This isn't common in framework Views because there are normally setters that take resource values as well as resource IDs, but you may find it in a custom View.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • I added some more info to my question. Your first line does not function, it gives an error at compile time. See my updated question. – Frank May 05 '17 at 12:07
1

you can do it:)

android:textColor="@{inputValue == null ? R.color.red: R.color.blue}"
redAllocator
  • 725
  • 6
  • 12
  • 2
    that, and I have to import R explicitly. – Frank May 03 '17 at 06:35
  • 1
    actually I think it shows the wrong color, whatever I put there "red" and "green" for example, it will always look blueish (different shades of blue). – Frank May 03 '17 at 06:43
  • It looks blueish because `R.color.red` is not a colour, it is colour ID. You have to process that ID through `Resources` or `ContextCompat` classes. – Jenea Vranceanu May 26 '20 at 12:29
0

It's a google bug: id and parameter cannot have the same name: parameter inputValue and view id @id/input_value. They become the same inputValue field in data binding. This is only a problem if you set a custom color. An error from Google, should be fixded in the next update.

Frank
  • 12,010
  • 8
  • 61
  • 78