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?
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
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.
this answer post AndroidX
androidx.core.view.MenuItemCompat.setContentDescription(menuItem, contentDescription)
It seems they just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O)
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);
}
Related
I have the below espresso test:
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
// if I Thread.sleep() here, I can see that the MenuItem has been clicked already
onView(withText("Sign in")) //<= click on the MenuItem
.perform(click());
onView(withId(R.id.signupButton)) //<= click the signup button in my UI
.perform(click());
The first line up there opens the overflow menu and clicks the first item at the same time (which happens to be the signin item). So the test fails because it cannot find the MenuItem view. Is there anything I am doing wrong ? I am using an emulator API 22, compiling agains targetSdk 24 and using espresso 2.2.1.
Try that:
public class EspressoMatchers {
public static Matcher<View> withOverflowMenuButton() {
return anyOf(allOf(isDisplayed(), withContentDescription("More options")),
allOf(isDisplayed(), withClassName(endsWith("OverflowMenuButton"))));
}
}
To open overflow menu:
onView(allOf(EspressoMatchers.withOverflowMenuButton(),
isDescendantOfA(withId(R.id.toolbar)))).perform(click());
Then it should work fine. Just use right ID for your Toolbar. I know this is just a copy from Espresso class, but I also ran into this issue and this helped me.
Please remember to always click the menu items "by name", not by their ID, as ID won't work. So your "click item" should be fine:
onView(withText("Sign in")) //<= click on the MenuItem
.perform(click());
I search on google and stackoverflow this topic.I ve found a few way to implement.
one of these is actionbarsherlock , but actually I do not understand how can implement this to my project. Is there any simple way? I mean a few classes or just add a library I do not know but I have a huge project and I want to implement this .Could you show me how can do it easily?
thanks
If you want to use ActionbarCompat library.
1) Import the ActionbarCompat library project into your workspace first and add the library to your project
https://developer.android.com/tools/support-library/setup.html#libs-with-res
2) Extend your Activity Class with ActionBarActivity
3) set your theme in manifest as
android:theme="#style/Theme.AppCompat"
Please check this link.
You can use android support library for this. No need of any other library.
Example also there in side link.
If You want to use ActionBar that supports devices with lower api..
you can do two things ...
1)Use the support Library(ActionbarCompat)
2)Use ActionBarSherlock
I use ActionBarsherlock
Steps to Use
1)YOURACTIVITY extends SherlockActivity
2) Use onCreateOptionsMenu to get the menu
`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
SubMenu subMenu1 = menu.addSubMenu("");
subMenu1.add(0,2,Menu.NONE,"Rate Us").setIcon(R.drawable.ic_action_good);
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_overflow);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
3) Use onOptionsItemSelected to get the item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
//rate app
break;
return super.onOptionsItemSelected(item);
}
4)Finally in your AndroidManifest File, add this under your activity
android:theme="#style/Theme.Sherlock"
`
5) and you are Done ...:)
For setup support library see-
https://developer.android.com/tools/support-library/setup.html
And for implementing action bar using support library see this-
http://antonioleiva.com/actionbarcompat-how-to-use/
Is it possible to have POPUP menu like play store attached to each row of adapter
what i done so far
holder.rl_overflow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, holder.rl_overflow);
popup.getMenuInflater().inflate(R.menu.overflow, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return false;
}
});
popup.show();
}
});
but constructor for POPUP menu says its available from API 11. I go through the developers.android.com and i found, its can be added using SUPPORT V7 library "https://developer.android.com/reference/android/support/v7/widget/PopupMenu.html" but i'm not able to implement this using ABS, please help someone.
Use like this PopupMenu in ActionBarSherlock.
Styling of the PopupMenu -
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
<style name="PopupMenu.MyAppTheme" parent="#style/Widget.Sherlock.ListPopupWindow">
<item name="android:popupBackground">#android:color/white</item>
</style>
If you are using the support library you should discard using the ABS. Instead, import the support library into your workspace which can be found in ~/adt-bundle-linux-x86_64-20130729/sdk/extras/android/support/v7/appcompact and use is in your project. And also don't forget to add the support library which can be brought up right clicking into your project and entering Android Tools -> Add Support Library
Using appcompact you will have to extend your activity class with ActionBarActivity. And also using the appcompact you have to make changes in your styles folder. You could refer to this. Do not also forget to update the values-v11 and values-v14 file. Doing all of this will make your application compatible.
P.S. If any error occurs in your appcompact library. Don't panic look at the error logs and open the file that seems to contain the error. Most probably you will have to refresh the file and after that you just fix project properties, and the error goes away.
Hope this helps :)
This is a picture of the facebook app from the play store. I would like to copy the part that says "New Stories 10+" in my app. How do I make that? Is that part of the action bar?
I read through the action bar documentation here but couldn't find anything about it. I'm having trouble doing a google search for it too because I don't know what it's called.
Also, if you open the facebook app when you're in airplane a very similar type of message comes up that says "No Internet Connection". How is that message created?
Looking a little more deeply at the Facebook app, the bar just appears to be a LinearLayout containing two TextViews. This layout is then just embedded in the news feed fragment and hidden/shown as needed. In other words, it's not a part of the action bar; it's just a normal view within the fragment.
Check out this library by Cyril Mottier. it was designed to do exactly what you are asking for.
Yes it is part of the ActionBar but only if the device or emulator is on API 3.0 and higher. if the device or emulator has a lower API level, the actions will appear on the menu button.
This is just a dummy but it will give you a rough idea.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
int groupid = 1;
menu.add(groupid, 1, 1, "Search").setIcon(R.drawable.embassy_default);
// menu.add(groupid, , ,"Back").setIcon(R.drawable.hospital_default);
menu.add(groupid, 1, 1, "Exit").setIcon(R.drawable.government_default);
return true;
}
Read more here
http://developer.android.com/guide/topics/ui/menus.html
In my onCreateOptionsMenu() I have basically the following:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_INSERT, Menu.NONE, R.string.item_menu_insert).setShortcut('3',
'a').setIcon(android.R.drawable.ic_menu_add);
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)){
menu.add(Menu.NONE, MENU_ITEM_SCAN_ADD, Menu.NONE, ((Collectionista.DEBUG)?"DEBUG Scan and add item":getString(R.string.item_menu_scan_add))).setShortcut('4',
'a').setIcon(android.R.drawable.ic_menu_add);
}
...
}
And in onPrepareOptionsMenu among others the following:
final boolean scanAvailable = ScanIntent.isInstalled(this);
final MusicCDItemScanAddTask task = new MusicCDItemScanAddTask(this);
menu.findItem(MENU_ITEM_SCAN_ADD).setEnabled(scanAvailable && (tasks == null || !existsTask(task)));
As you see, two options items have the same drawable set (android.R.drawable.ic_menu_add). Now, if in onPrepareOptionsMenu the second menu item gets disabled, its label and icon become gray, but also the icon of the first menu item becomes gray, while the label of that first menu item stays black and it remains clickable. What is causing this crosstalk between the two icons/drawables? Shouldn't the system handle things like mutate() in this case?
I've included a screenshot:
http://www.curious-creature.org/2009/05/02/drawable-mutations/
The above article by Romain Guy explains this very situation and provides a work around.
Yes, this looks odd. I can not explain why this is as it is, however I can propose a workaround - instead of using internal drawable resourse, you could put the same image in your app drawable resourse dir AND you could duplicate the add image, so you have 2 images - add_for_menu_item_1.png and add_for_menu_item_2.png named differently, but having the same visual representation. I am sure this would do the trick.
Could it be that both menu items are sharing the same alphaChar which is causing the second menuItem to be disabled?