Search code examples
androidaccessibilitymenuitemtalkback

Content description for Menu item - Android


I am trying to change accessibility content description for Android menu item. Here is my code and talk back announcing => Test search, Search, double tap to activate.

<item
       android:id="@+id/menuItemSearch"
       android:icon="@drawable/search"
       android:iconTintMode="src_atop"
       android:title="Search"
       android:visible="false"
       app:iconTint="@color/primary"
       app:contentDescription="Test Search"
       app:showAsAction="always"/> 

How can change it to => Search. Button. Double tap to search.


Solution

  • Solution

    Add -> app:actionViewClass="android.widget.ImageButton"

    <item
        android:id="@+id/menuItemSearch"
        android:icon="@drawable/icongel_search"
        android:iconTintMode="src_atop"
        android:title="@string/toolbarSearchIcon"
        android:visible="false"
        app:iconTint="@color/primary"
        app:showAsAction="always"
        app:actionViewClass="android.widget.ImageButton"/>
    

    Then

    menu.findItem(R.id.menuItemSearch).apply {
                                val searchIcon = this.actionView as ImageButton
                                searchIcon.apply {
                                    setImageResource(R.drawable.search)
                                    setColorFilter(ContextCompat.getColor(context, R.color.primary), PorterDuff.Mode.SRC_ATOP)
                                    contentDescription = getString(R.string.toolbarSearchIcon)
                                    setBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
                                    ViewCompat.setAccessibilityDelegate(
                                        this,
                                        object : AccessibilityDelegateCompat() {
                                            override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfoCompat) {
                                                super.onInitializeAccessibilityNodeInfo(host, info)
                                                info.addAction(
                                                    AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                                                        AccessibilityNodeInfoCompat.ACTION_CLICK, getString(R.string.toolbarSearchIcon)
                                                    )
                                                )
                                            }
                                        }
                                    )
                                }
                            }