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?

NamNH
  • 1,752
  • 1
  • 15
  • 37
  • What happens when you set btn.setContentDescription("Menu: " + "Create blabla"); and then btn.requestFocus() ? – JSBach May 03 '16 at 14:30

5 Answers5

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://stackoverflow.com/a/57950952/1236327

tim.paetz
  • 2,635
  • 20
  • 23
  • 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? – Alan Nelson Mar 23 '20 at 17:45
  • 1
    You should probably be using AndroidX instead of the V4 Support libs at this point. Please see https://stackoverflow.com/a/57950952/1236327 for AndroidX support. You will need to include the AndroidX dependency in your build.gradle file. – tim.paetz Mar 23 '20 at 20:18
13

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.

NamNH
  • 1,752
  • 1
  • 15
  • 37
  • Did you come up with a possible solution or idea? Can we overwrite it with an extend? Because I have, I believe, similar issue, http://stackoverflow.com/questions/36971528/android-seekbar-talkback-talking-too-much – JSBach May 11 '16 at 15:18
  • Sorry for late feedback, but I still have no solution on my issue. – NamNH May 16 '16 at 02:21
  • 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. – JSBach Jul 18 '16 at 01:00
7

this answer post AndroidX

androidx.core.view.MenuItemCompat.setContentDescription(menuItem,  contentDescription)
Amit
  • 3,422
  • 3
  • 28
  • 39
2

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

dasfima
  • 4,841
  • 3
  • 21
  • 24
  • Yeah I saw this method is now available on `Android O Developer Preview` but not tested. But, I hope this will work. :). – NamNH Jun 22 '17 at 09:15
  • 3
    also, adding `app:contentDescription` (not `android:contentDescription`) to the menu's XML seems to work on earlier versions for me. – dasfima Jul 26 '17 at 11:03
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);
}
Thiago
  • 12,778
  • 14
  • 93
  • 110