Android Menu Icon Tint (Kotlin)

May 10, 2018
Change MenuItem icon color

By XML

Use app:iconTint.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_share"
        android:title="Share"
        android:icon="@drawable/ic_share_black_24dp"
        app:iconTint="@android:color/white"
        app:showAsAction="ifRoom" />
</menu>        

NOTE: If you use android:iconTint, it will complaint of Attribute iconTint is only used in API level 26 and higher.

By Code

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_test, menu)

    // val menu = menu.findItem(R.id.action_layout)

    for (i in 0 until menu.size()) {
        var menuItem = menu.getItem(i)

        val icon = menuItem.icon
        if (icon != null) {
          DrawableCompat.setTint(icon, ContextCompat.getColor(this, android.R.color.white))
        }
    }

    // return true
    return super.onCreateOptionsMenu(menu)
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.