I'm trying to set TextView
text color using data binding library
android:textColor="@{holder.getTitleColor(context, item)}"
where the method in Holder
class is defined like below
public int getTitleColor(Context context, Item item) {
...
}
No matter if I return color int (@ColorInt
) or color resource (@ColorRes
) it paints the text solid white. What am I doing wrong?
Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.
Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
On android application there are multiple ways to change TextView text color using layout file and programming file. Developer can change text using both methods but defining color using activity_main.xml file is static method and declaring text color using MainActivity.java programming file is the dynamic method.
Use android.graphics.Color class to get an integer for a given color. You can provide the color as hex value in one of the four formats: rgb, argb, rrggbb, or aarrggbb. The syntax to set text color using setTextColor () method of TextView in Kotlin Activity file is var textView = findViewById<TextView> (R.id.text_view_id)
Data binding allows you to write expression handling events that are dispatched from the views (for example, the onClick() method). Event attribute names are determined by the name of the listener method with a few exceptions. For example, View.OnClickListener has a method onClick(), so the attribute for this event is android:onClick.
setTextColor () method takes int as argument. Use android.graphics.Color class to get an integer for a given color. You can provide the color as hex value in one of the four formats: rgb, argb, rrggbb, or aarrggbb.
I seems the int
you are providing is interpreted as a hex color, even though it seem intuitive that this setter should be expecting a resource ID.
use the Context
reference generated for each bindable view, and use it to convert the resource ID to the color you are pointing to, as described in the DataBinding Dev Guide:
A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext().
use it to set color like this:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{data.text}" android:textColor="@{context.getColor(data.colorRes)}" />
for backwards compatibility you can use ContextCompat. Import needed:
<layout> <data> <import type="android.support.v4.content.ContextCompat"/> <variable name="data" type="com.whatever.myapp.MyModel"/> ... </data> ... <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{data.text}" android:textColor="@{ContextCompat.getColor(context, data.colorRes)}" /> </layout>
create method by using BindingAdapter
@BindingAdapter({"bind:color"}) public static void setColor(TextView textView, Item item) { textView.setTextColor(<set color of your choice>); }
and to call it from xml
app:color="@{item}"
In addition to the solution of @Mardann, here is an updated solution that also works on API lower than 23 by using ContextCompat.getColor():
<layout>
<data>
<import type="androidx.core.content.ContextCompat" />
<variable
name="data"
type="com.example.myapp.MyDataObject" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.text}"
android:textColor="@{ContextCompat.getColor(context, data.colorRes)}"/>
</layout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With