Show icons in custom text selection menu in android

Viewed 213

I want to display icons in custom text selection menu. I've implemented the "Custom Selection Action Mode Callback" methods and inflate my custom menu content like this:

myTextView.customSelectionActionModeCallback = object : ActionMode.Callback {

    override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
        val menuInflater: MenuInflater = mode.menuInflater
        menu.clear()
        menuInflater.inflate(R.menu.text_selection, menu)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
        return true
    }

    override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
        return true
    }

    override fun onDestroyActionMode(mode: ActionMode) {

    }
}

text_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item1"
        android:title="item1"
        android:icon="@mipmap/icon1"
        app:showAsAction="always" />
    <item
        android:id="@+id/item2"
        android:title="item2"
        android:icon="@mipmap/icon2"
        app:showAsAction="always" />
    <item
        android:id="@+id/item3"
        android:title="item3"
        android:icon="@mipmap/icon3"
        app:showAsAction="always" />
</menu>

But only the titles (item1, item2,...) appear on menu. I want to display icons like this image (I only want the 1st row in this image):

context menu

0 Answers
Related