Reputation: 8408
Why does Kotlin not allow me to apply a color filter to the overflow icon in my Toolbar?
Expression 'colorFilter' of type 'ColorFilter' cannot be invoked as a function. The function 'invoke()' is not found
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:id="@+id/toolbar_textLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginStart="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
Kotlin
mToolbar.overflowIcon?.colorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP)
//
val myAttr = intArrayOf(R.attr.tintColor)
val taAttr = this.theme.obtainStyledAttributes(myAttr)
val colorAttr = taAttr.getColor(0, Color.BLACK)
Upvotes: 1
Views: 1711
Reputation: 1007359
TL;DR: Change colorFilter()
to setColorFilter()
.
There are two setColorFilter()
methods on Drawable
.
One takes a ColorFilter
. That matches a corresponding getColorFilter()
method. Kotlin treats those as being syntactically equal to a var
named colorFilter
. So, if you had a ColorFilter
object, you could write:
mToolbar.overflowIcon?.colorFilter = myReallyCoolColorFilterNoReallyItAddsBlueTintToEverything
The other setColorFilter()
call takes the two parameters that you are specifying: the color and the PorterDuff.Mode
. However, that method is setColorFilter()
, not colorFilter()
. So, either switch to setColorFilter()
or:
mToolbar.overflowIcon?.colorFilter = BlendModeColorFilter(Color.RED, BlendMode.SRC_ATOP)
Since you are trying to reference colorFilter
, Kotlin assumes that you mean the colorFilter
property, and that is not a function type and cannot be called like one.
Upvotes: 2