From the course: Android Development: Data Binding

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Color binding adapter

Solution: Color binding adapter - Android Tutorial

From the course: Android Development: Data Binding

Start my 1-month free trial

Solution: Color binding adapter

(upbeat music) - [Instructor] All right. Let's take a look at how I built my solution. And again, remember, it's not important for your solution to match mine. Okay. So I chose to solve this problem by adding a binding adapter, which I said we were going to use, and I put it into the existing binding adapters file. And so what I did was I created a function named tempToColor, and it takes the view and it takes the integer for the value, and you can see that for the binding adapter decoration, I'm declaring that the adapter is used when it is applied to the textColor attribute on the view. So the code itself is pretty straightforward, right? It fits just a few lines. So when the temperature is below 15, the text color is set to the Color.BLUE constant, and when it's equal to or above 22, I use Color.RED. And unfortunately there's no predefined color constant for the orange color. So I had to make my own, and I did…

Contents