Here's a blog post that has been a draft for way too long: an example on using custom states in Android components. I hope you find it useful!
Example of custom states in Android components |
A while ago, I built a form based app with lots of input fields and all the things that come with that, like validation and error messages. In one of the situations, if a section is invalid, the field labels should turn red. Easy right? Here's the example layout: ...
154 plus ones
Shared publicly•View activity
- I didn't even know this was possible :-)
Very cool, thanks.Jul 22, 2013
- Definitely noJul 22, 2013
- <removed some weird and spammy comments>Jul 23, 2013
- Thanks for sharing! It seems to me like this approach will make all the labels red. That doesn't seem as useful or as realistic as turning just the fields with errors red. It doesn't seem to me as if an example with one label that turns red would be more complicated.Jul 23, 2013
- No, it will make all labels red which have the proper selector set (note the difference). So you can use this as any other linear layout in that sense. For my use case, I had a bunch of separate fields that should light up red and a header in the same container. Secondly, the view is now properly decoupled from the code. If different components need different colors or behaviors dependent on the error state it's easy to do so.
Ultimately, the main take away from this should be that you can update view state by implementing a custom state and thus reduce the amount of logic and code in your own classes because the Android framework will take care of it. Even if you implement it on a single view, like extending a TextView for this purpose.Jul 23, 2013
- OK, got it. Thanks again!Jul 23, 2013
- Pretty cool, thx for sharing!Jan 18, 2014
- Thanks for the explanation it cleared some confusion for me. I have not tried this approach yet, but will do in a while..May 29, 2015
Add a comment...