Setting content description for option menu android Talkback
Asked Answered
R

5

12

I am implementing "Read Aloud" or "Talkback" for an app. Everything is working with contentDescription text, but with option menu, I found nothing related to contentDescription, I want system read "Menu "+ item's name.

EX: My menu has 2 items: "Create New Folder" and "Delete current folder", currently, when I focus a menu item (Support trackball and bluetooth key), system can talk exactly the menu's text. But I want it talks more like "1: Menu Create New Folder" and "2: Menu Delete current folder".

So, How can I change the read text? How can I get the focused menu item when bluetooth keyboard press UP/DOWN key?

Resplendent answered 22/4, 2016 at 15:51 Comment(1)
What happens when you set btn.setContentDescription("Menu: " + "Create blabla"); and then btn.requestFocus() ?Lang
R
14

As my investigation, in Android internal source code, class ActionMenuItemView.java method setTitle(CharSequence title), the source code also sets setContentDescription(title), so Android will read your MenuItem's text as default. I don't know why the core has so inflexible in this case.

Updated:

Thanks for @sofakingforever answer.

Seem Google just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O).

Updated: Thanks for new @tim.paetz answer . Look like all versions are now supported setContentDescription for menu item using android support v4 libraries.

Resplendent answered 4/5, 2016 at 3:31 Comment(3)
Did you come up with a possible solution or idea? Can we overwrite it with an extend? Because I have, I believe, similar issue, #36972028Lang
Sorry for late feedback, but I still have no solution on my issue.Resplendent
no worries. yes I don't have too. So made an empty label and moved on. As it seems, Spotify couldn't have solved it too. They had the similar problem when I checked.Lang
B
17

MenuItemCompat in the v4 support libraries has a

android.support.v4.view.MenuItemCompat.setContentDescription(MenuItem menuItem, CharSequence contentDescription) 

method for backwards compatibility on pre-Oreo devices.

For AndroidX see this answer:

https://mcmap.net/q/890648/-setting-content-description-for-option-menu-android-talkback

Bibb answered 28/9, 2017 at 20:34 Comment(2)
Can you provide a better example for this? The 'view' portion of that code is not recognized by my android studio. Perhaps it needs to be imported at the top of the page and in the gradle.app?Litigate
You should probably be using AndroidX instead of the V4 Support libs at this point. Please see https://mcmap.net/q/890648/-setting-content-description-for-option-menu-android-talkback for AndroidX support. You will need to include the AndroidX dependency in your build.gradle file.Bibb
R
14

As my investigation, in Android internal source code, class ActionMenuItemView.java method setTitle(CharSequence title), the source code also sets setContentDescription(title), so Android will read your MenuItem's text as default. I don't know why the core has so inflexible in this case.

Updated:

Thanks for @sofakingforever answer.

Seem Google just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O).

Updated: Thanks for new @tim.paetz answer . Look like all versions are now supported setContentDescription for menu item using android support v4 libraries.

Resplendent answered 4/5, 2016 at 3:31 Comment(3)
Did you come up with a possible solution or idea? Can we overwrite it with an extend? Because I have, I believe, similar issue, #36972028Lang
Sorry for late feedback, but I still have no solution on my issue.Resplendent
no worries. yes I don't have too. So made an empty label and moved on. As it seems, Spotify couldn't have solved it too. They had the similar problem when I checked.Lang
N
7

this answer post AndroidX

androidx.core.view.MenuItemCompat.setContentDescription(menuItem,  contentDescription)
Nolanolan answered 16/9, 2019 at 5:42 Comment(0)
A
2

It seems they just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O)

Anacoluthon answered 22/6, 2017 at 8:53 Comment(2)
Yeah I saw this method is now available on Android O Developer Preview but not tested. But, I hope this will work. :).Resplendent
also, adding app:contentDescription (not android:contentDescription) to the menu's XML seems to work on earlier versions for me.Anacoluthon
T
1

Full sample:

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.client_menu_close, menu);
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem closeMenu = menu.findItem(R.id.client_menu_close_action);
    androidx.core.view.MenuItemCompat.setContentDescription(closeMenu, R.string.str_accessibility_client_screen_close);
}
Taken answered 14/9, 2020 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.