Android FAB setBackgroundTintList with ColorStateL

2019-08-01 20:24发布

问题:

I want to change the colour of the NFC scanning FAB when NFC is not enabled. I've managed to successfully change the colour when the app launches, but if the user taps the FAB and enables NFC, the colour isn't changed to the primary colour. The logs say it does, but the change doesn't happen.

My minSdkVersion = 15

XML:

<android.support.design.widget.FloatingActionButton
     android:id="@+id/fab_scan_nfc"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     app:backgroundTint="@color/colorPrimary"
     app:borderWidth="0dp"
     app:elevation="6dp"
     app:fabSize="mini"
     app:fab_colorNormal="@color/colorPrimary"
     app:fab_colorPressed="@color/colorPrimaryDark"
     app:fab_colorRipple="@color/colorAccent"
     app:srcCompat="@drawable/ic_nfc_n" />

Kotlin:

if (!isNFCEnabled) {
    Log.d(TAG, "Change NFC fab colour to disabled.")
    fab_scan_nfc.backgroundTintList = ColorStateList.valueOf(R.color.colorDisabled)
} else {
    Log.d(TAG, "Change NFC fab colour to primary.")
    fab_scan_nfc.backgroundTintList = ColorStateList.valueOf(R.color.colorPrimary)
}

This is the same as Java:

if (!isNFCEnabled) {
    Log.d(TAG, "Change NFC fab colour to disabled.")
    fab_scan_nfc.setBackgroundTintList = ColorStateList.valueOf(R.color.colorDisabled);
} else {
    Log.d(TAG, "Change NFC fab colour to primary.")
    fab_scan_nfc.setBackgroundTintList = ColorStateList.valueOf(R.color.colorPrimary);
}

As a sideline - when the code does apply the disabled colour background tint, there is this smaller circle on the FAB which looks like a touch indicator. Does this have something to do with using a colour state list ?