首页 > 解决方案 > How to customise Floating Action Button to have different look and feel for enabled and disabled FAB state in Android?

问题描述

There is no difference in appearance when FAB is disabled or enabled. How can I achieve a different look and feel for enabled and disabled FAB via xml?

标签: androidstylesfloating-action-button

解决方案


You could change the icon when the button is enabled/disabled? For example, if your FAB has an image of a "plus", change this to a red "plus" when disabled and green when enabled.

To do this, you will need to keep track of the state of the FAB.

private var fabEnabled = true

override fun onCreate(savedInstanceState: Bundle?) {
...

fab.setOnClickListener { view ->
    if (fabEnabled) {
        fab.setImageResource(R.drawable.ic_green_plus)
    } else {
        fab.setImageResource(R.drawable.ic_red_plus)
    }
}

Change fabEnabled as you wish, such as via another button.

Note this answer is in Koltin. Please tell me if you would rather it in java.


推荐阅读