Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding selector in TextView textColor

I am trying to set colors from a textview based on the number of unread messages in a channel. Like so:

android:textColor="@{channel.unreadCount > 0 ? @color/selector_conversation_row_title_unread : @color/selector_conversation_row_title_read}"

this only sets the color of the title, while:

android:textColor="@color/selector_conversation_row_title_unread"

this code sets the textColor as a selector, and if i press the TextView the color changes unlike the first statement.

selector_conversation_row_title_unread:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/colorConversationTitleUnread"/>
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
    <item android:color="@color/colorConversationTitleUnread"/>
</selector>

selector_conversation_row_title_read:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/colorConversationTitle"/>
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
    <item android:color="@color/colorConversationTitle"/>
</selector>

Why does the selector only work as?:

android:textColor="@color/selector_conversation_row_title_unread"
like image 539
Arnout Avatar asked Sep 13 '16 14:09

Arnout


People also ask

Should I use DataBinding or ViewBinding?

In short you can use ViewBinding to replace findviewbyid() effectively. If your project is however more complex and you need to add features like binding data to views, binding adapters e.t.c, use DataBinding.

What is DataBinding and view binding?

View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.

What attribute changes the color of TextView?

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.


Video Answer


1 Answers

Android Data Binding doesn't know about resource types, so you must supply it in the expression:

android:textColor="@{channel.unreadCount > 0 ? @colorStateList/selector_conversation_row_title_unread : @colorStateList/selector_conversation_row_title_read}"
like image 122
George Mount Avatar answered Sep 21 '22 08:09

George Mount