Using the VectorDrawable

We are first going to create our Drawables variables in the DetailActivity.kt

val animDrawable = getDrawable(R.drawable.avd_edit_done) as AnimatedVectorDrawable
val animDrawable2 = getDrawable(R.drawable.avd_done_edit) as AnimatedVectorDrawable

And then we need to assign the drawables to our FAB like this:

DetailActivity.kt
private var isEditMode = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        fab.setImageDrawable(animDrawable)
        fab.setOnClickListener {
            if (!isEditMode) {
                fab.setImageDrawable(animDrawable)
                (fab.drawable as Animatable).start()
            } else {
                fab.setImageDrawable(animDrawable2)
                (fab.drawable as Animatable).start()
            }
            isEditMode= !isEditMode
        }
    }

Note that we're also using a flag to know when we should turn our FAB icon into a pencil or a check mark.

Last updated