Member-only story
Android KTX resolve custom view attributes with style
A quick hint on how to safely resolve attributes in your custom views with kotlin
Androids KTX extension functions frequently come to the rescue in situations wherein the old days, Java had forced us almost always to write multiple lines of code even for the smallest repetitive tasks.
Now with the luxury of extension functions in Kotlin, many times we can save these lines and therefore come often (but not always) to better readability and maintainability of our code.
One of these is the withStyledAttributes
extension function that I want to talk about in this article.
The old way
First, we want to set up a very simplistic custom view. It’s just a small compound view with a TextView
and is absolutely useless in production:
Then we create our custom attributes in our values folder: