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.
There are 2 issues here:
You need to import the latest material library in your app's build.gradle
file.
implementation 'com.google.android.material:material:1.7.0'
Be wary as there may be other dependencies.
Currently this is not possible as you would need to get access to the view in the Toolbar. Then you could use the following method as described in the documentation :
ViewCompat.replaceAccessibilityAction(
// View that contains touch & hold action
itemView, // <-- this is what we don't have
AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_LONG_CLICK,
// Announcement read by TalkBack to surface this action
getText(R.string.favorite),
null
)
You could probably raise a bug on the Issue Tracker for problem 2.
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) ) ) } } ) } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.