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.
Related
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/
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);
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 enabled the ShareActionProvider and I get this error:
java.lang.NoSuchMethodError: android.view.MenuItem.getActionProvider
But the way I use this class is like this:
// SHARING ONLY ENABLED in SDK 14 which is Ice Cream Sandwich
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
Button share = (Button)findViewById(R.id.share_button);
share.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
openOptionsMenu();
}
});
}
else
{
// HIDE THE TWO PAGE ELEMENTS
Button share = (Button)findViewById(R.id.share_button);
TextView share_prompt = (TextView)findViewById(R.id.share_prompt);
share.setVisibility(View.GONE);
share_prompt.setVisibility(View.GONE);
}
}
catch ( Exception e )
{
}
So I thought that I would not show the share button for earlier sdk's and I would be ok. But I am getting a lot of crashes.
I can not really test this because I don't have a phone with an earlier version of the SDK. But does it mean that these pages crash for everyone who has the earlier SDK version? Or just people who click share? How do I prevent this crashing?
And I have these methods in the class. Should I just not run them if sdk is less than 14?
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.layout.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"Some text");
return shareIntent;
}
// Somewhere in the application.
public void doShare(Intent shareIntent)
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
Thanks.
Alex
ShareActionProvider is available only for API 14+ which means in earlier versions you can't use it. If you want to add share button in your app and support old API levels I can suggest you to use ActionBarSherlock - a library which gives you the opportunity to use ActionBar in older versions of Android. Using this library you can do something like this to add share button :
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
Which will share an image file. If you don't want to support older API levels I would suggest you just check for API level and depending on that use ShareActionProvider.
Hope this help.
Yes, you need to have those same checks for your menu items. See the docs for getActionProvider. It's only supported on 14+. To test this kind of thing without a device, you can try on an emulator that uses an older version of Android.
In documentation you can see that MenuItem.getActionProvifer() from 14 API, of course you will catch java.lang.NoSuchMethodError on older versions (that errors you will catch every time when use methods or classes from new API).
Using try-catch can't save you:) You need to use different implementations with workarounds hanler-classes for different critical API versions.
If you will use ActionBarSherlock, you can use that method in Android API >= 8. Try to save functional for maximem API, it will be great!
A little late....
ShareActionProvider and in general ActionProviders are added from Android 4.0 (API Level 14) to Android Framework.
But the AndroidSupportLibrary helps us to support this feature in previous versions as well. But generally we do a mistake importing the ShareActionProvider from the Android SDK framework but not the SupportLibrary.
CODE:
-->In the main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_share" android:title="#string/menuitem_detail_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Note: Observe that the 'app' namespace is used and not 'android' namespace'
WHY? => This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
-->In the Activity's onCreateOptionsMenu() :
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "The Text you want to share);
mShareActionProvider.setShareIntent(shareIntent);
Note: ShareActionProvider is imported from "import android.support.v7.widget.ShareActionProvider" and observe the usage of "MenuItemCompat.getActionProvider()" to get the ActionProvider instance.
You need to add condition to your when you support lower APIs while still using higher API calls. IT will fail in such case as there's simply no such method yet. And I'd suggest to slightly improve readability for your code, by replacing lines like:
if ( android.os.Build.VERSION.SDK_INT >= 14 )
with
if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWITCH )
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);