Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text color using data binding on Android

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?

like image 969
tomrozb Avatar asked Oct 07 '16 06:10

tomrozb


People also ask

How do I change the text color on my Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

Is Data Binding good in Android?

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.

What is property changes the color of text Android?

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.

How to change text color in textview on Android?

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.

How to set text color in Kotlin activity in Android?

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)

What is data binding in Android with example?

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.

How to get hex value of a given color in Android?

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.


3 Answers

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)}"             /> 

Edit

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> 
like image 106
Mardann Avatar answered Sep 16 '22 21:09

Mardann


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}" 
like image 29
Ravi Avatar answered Sep 17 '22 21:09

Ravi


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>
  • Make sure to import ContextCompat as shown above.
  • You can automagically 'context' as method parameter for ContextCompat.getColor() because it will be automatically resolved to the view's context.
like image 31
Mehlyfication Avatar answered Sep 20 '22 21:09

Mehlyfication