I'm using the appcompat7 lib for ActionBar backwards compatibility. Now I have a MenuItem that I retrieve, and then want to set an ImageView myView as its icon.
The way how to do it from API level 11 is:
MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh.setActionView(myView);
For API levels lower than 11 this doesn't work, the second line will show an error. Is there an option to do this in compatibility mode?
Look at MenuItemCompat:
http://developer.android.com/reference/android/support/v4/view/MenuItemCompat.html
There is a static function setActionView(MenuItem item, View view)
So your code should look like:
MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh = MenuItemCompat.setActionView(menuItemRefresh, myView);
Related
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);
}
How to disable api level check for a specific code line that is giving the following error:
Call requires API level 14 (current min is 11): android.view.MenuItem#getActionProvider
this is the where it is giving the error getActionProvider
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_server_status, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
if(android.os.Build.VERSION.SDK_INT >= 14) {
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
}
return true;
}
this is a menu that should work for all APIs 11+, if it is 14+ then the share button should work normally else i will hide that button from the menu or even load another menu.
I tried using #TargetApi but when I did that i tried to create another onCreateOptionsMenu but that is not possible to create two functions with the same name even when I used the #TargetApi
Even better is there a share button that works with API 11+?
I tried using #TargetApi
That is the right answer.
when I did that i tried to create another onCreateOptionsMenu
You do not need another onCreateOptionsMenu() method.
Even better is there a share button that works with API 11+?
android.widget.ShareActionProvider works on API Level 14+. android.support.v4.view.ShareActionProvider works on API Level 4+, if are using the AppCompat action bar backport.
Note that there are virtually no devices running API Level 11-13, so worrying about 11 is not especially relevant today.
I want to show/hide ProgressBar in ActionBar on all android devices.
I am using android support library (android-support-v7-appcompat).
My activity extends ActionBarActivity and in onCreate it requests window feature supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); (before setting content).
On my button click I show/hide ProgressBar.
public void onClick(View v) {
if(v.getId() == R.id.buttonProgress) {
if(progress) {
setProgressBarIndeterminateVisibility(false);
progress = false;
} else {
setProgressBarIndeterminateVisibility(true);
progress = true;
}
}
}
This code works fine on android API higher than 11. But I have proglem with API lower than 11. The ProgressBar is not showing up. There is no error in LogCat.
I have noticed that when I show ProgressBar in onCreate it works. I also can hide it from onCreate.
Do you have solution for this problem?
Thank you!
call
setSupportProgressBarIndeterminateVisibility(true)
if call it from fragment cast the activity for example:
ActionBarActivity ac =(ActionBarActivity) getActivity();
ac.setSupportProgressBarIndeterminateVisibility(true);
There is no action bar for apps lower than API 11.. If you use ProgressBar for Less than API 11 (Honeycomb) it does show up but it will tiny circle revolving on the top right of the title bar (thin bar on top of the app). Well, that depends on the theme.
If you want an actionbar you may want to look into an external library : ActionBarSherlock
The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
And ActionBar is added in support liabrary to allow implementation of the action bar user interface design pattern back to Android 2.1 (API level 7) and higher. Use of this class requires that you implement your activity by extending the new ActionBarActivity class.
Have a look at the official document here
However you can achieve this by using ActionBarSherlock library.
Check out this
Site for ABS Here you can get the sample programs ABS Library
I see this question sets focus on the SearchView EditText when I activate a search from the ActionBar. However the keyboard does not come up when it gains focus. Shouldn't it, as it is just a normal EditText? (Is it a normal EditText?) This behaviour is seen on Android SDK level 11. (Samsung Galax Tab 7.7 with stock Android.)
I have a workaround at the moment that hooks in on the onOptionsItemSelected(MenuItem item) method of my Activity, showing the keyboard.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean menuSelectionHandeled = super.onOptionsItemSelected(item);
// menu_search is the id of the menu item in the ActionBar
if (item.getItemId() == R.id.menu_search) {
mInputManager.showSoftInput(null, InputMethodManager.SHOW_IMPLICIT);
}
return menuSelectionHandeled;
}
Where mInputManager is an instance of InputMethodManager.
The ActionBar is built with ActionBarSherlock, and since the target device is Android 3.x could this be the cause of the symptoms? As per ActionBarSherlock's FAQ :
The action bar on Android 3.x (also known as Honeycomb) does not
implement all of the features of the one in Android 4.x (Ice Cream
Sandwich). In order to provide a full action bar API on all platforms
as well as unify the styling across all versions of Android the custom
implementation is used.
This should work :
SearchView searchView = new SearchView(getContext());
searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.setBackgroundColor(Color.WHITE);
Is there action bar for Android Level 7 or something else that I can use as action bar?
I need to build an app which is using action bar for Android 2.1
JohanNilsson has actually created a ActionBar library which is available on GitHub.
Direct link.
here you go. The officle android article is called: ActionBarCompat - Action Bar Compatibility. (just in case the link wont work for ever)
Well, you should try this open source project http://actionbarsherlock.com , you will find it's full definition on this website
In the newest version it will have support for features provided by ICS.
TypedArray a = mActivity.getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {
R.attr.actionbarCompatItemHomeStyle,
R.attr.actionbarCompatItemStyle
});
int actionbarCompatItemHomeStyle = a.getResourceId(0, 0);
int actionbarCompatItemStyle = a.getResourceId(1, 0);
ImageButton actionButton = new ImageButton(mActivity, null,
itemId == android.R.id.home ? actionbarCompatItemHomeStyle : actionbarCompatItemStyle
);
The equivalent of action bar in pre-Honeycomb versions of Android is the options menu, accessible under Menu button.
In July 2013 Google added action bar compatibility back to 2.1 (7+) in New "v7 appcompat library"
http://developer.android.com/tools/support-library/index.html