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 - Android Tutorial
From the course: Android Development: Data Binding
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Overview of observables1m 51s
-
(Locked)
Binding to observables4m 49s
-
(Locked)
Binding to ViewModels6m 25s
-
(Locked)
Using binding adapters7m 19s
-
(Locked)
Two-way binding syntax4m 45s
-
(Locked)
Using converters4m 2s
-
(Locked)
Challenge: Color binding adapter1m 18s
-
(Locked)
Solution: Color binding adapter2m 26s
-
(Locked)
-