Log.v(“ThreeTips”, ”#15”)

1 — Mutate your drawable!

Let say you have to change filter of drawable. What would you do? It is an easy question. You will do something like that.

val drawable = ContextCompat.getDrawable(context, R.drawable.icon)

Everything seems OK. Until you need to use same resource in different page.I want to share official documentation about Drawable.

All drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

So If you load R.drawable.icon in another activity you will see latest modification which is applied previous activity.
There is a method in Drawable class to prevent this, mutate().

This method makes drawable mutable and this operation can not be reversed. A mutable drawable is guaranteed to not share its state with any other drawable.

ContextCompat.getDrawable(context, R.drawable.icon).mutate()

2 — Databinding meets Kotlin

In my recent project, I tried to enable databinding but It didn’t work. When you writing your app in Java, only thing that you need to do is enabling databinding just like following

dataBinding {
enabled true

In kotlin, you also need to add databinding processor to your build.gradle.

//version must be the same than our gradle version
kapt 'com.android.databinding:compiler:3.0.0'

3 — Be aware of style trick

I have a question for you.

<style name="BigButton"/>

<style name="BigButton.Awesome"/>

<style name="BigButton.Great" parent="SmallButton"/>

What is the parent of BigButton.Great style? BigButton or SmallButton ? or both?
The answer is SmallButton.

If you declare style, you can define its parent in two ways. One of them is defining them with dot notation. Just like in the “BigButton.Awesome” style. Another way is define them with “parent” attribute. But when you combine them both, parent attribute takes precedence. So don’t mix both parenting rules. It leads to confusion about styles. When using “BigButton.Great” in your xml, developer thinks that it is BigButton but when he digs into style, realises that it is Small. So It leads to an illusion, don’t mix parenting!



Software Engineer at Storytel

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store