API Level error - android

In an Activity I have a code that shows this error, but only if you press save.
Call requires API level 11 (current min is 8): android.widget.SearchView#setSearchableInfo
If I change the android:minSdkVersion to 7, it works, but when I save the code again, the same error is thrown. The minSdk must then be changed back to 8,...
What is wrong?

SearchView is available since API lvl 11.
Since your minimum sdk is 8 (lower than 11), Lint will give an error when using SearchView.
You can remove that error by using #TargetApi annotation before your method or class.
But you have to make sure you use a conditional statement before using SearchView to check if it is available, and provide an alternative for earlier versions.
Here's what your code should look like:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
void yourMethod(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// use SearchView
} else {
// use some other backward compatible custom view
}
}

The SearchView exists in Android from the version 11 ant more.
So, if you would like to use the SearchView in your code, you have to put the minSdkVersion in your manifest to be 11. In the case you put a number smaller than 11, you will get an error, which is normal beause you are giving access to your app to some android versions which will not support your app.
This can be seen (thanks to #JesseJ) here: http://developer.android.com/reference/android/widget/SearchView.html
Added in API level 11

Related

MVP-Mosby-Api10: NoSuchMethodError android.support.v4.app.FragmentActivity.isChangingConfigurations

I get this error on crashlytics panel:
Fatal Exception: java.lang.NoSuchMethodError
android.support.v4.app.FragmentActivity.isChangingConfigurations
com.hannesdorfmann.mosby.mvp.MvpFragment.shouldInstanceBeRetained (MvpFragment.java:91)
com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.detachView (MvpInternalDelegate.java:70)
com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegateImpl.onDestroyView (FragmentMvpDelegateImpl.java:73)
com.hannesdorfmann.mosby.mvp.MvpFragment.onDestroyView (MvpFragment.java:106)
com.hannesdorfmann.mosby.mvp.MvpFragment.shouldInstanceBeRetained (MvpFragment.java:91)
I override manifest for library to use it with api level 10 and I already test it on android 2.3.3 and it was working ok! but now I see this crash on crashlytics. Hi I can fix this for my version? is crash related to api 10? because the method is for support v4 library so I can't understand why this occurred.
yes the method isChangingConfigurations() has been introduced with API 11:
https://developer.android.com/reference/android/app/Activity.html#isChangingConfigurations()
as part of theandroid.app.Activity plattform class (and not as part android.support.v4.app.FragmentActivity, but FragmentActivity extends Activity).
Hence, this won't work on API < 11.
You could implement isChangingConfigurations() in your Activity and either call
super.isChangingConfigurations() if API >=11 or implement your own thing if (API < 11). You may want to take a look at Activities source code, but I'm not sure how this could be back ported. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java#L5152
You could try to just return false if API < 11 . That would mean that the View's state (and Presenter) will not survive screen orientation changes. DISCLAIMER: That might also cause some other unwanted side effects I'm not aware of right now and could break with any future release of Mosby or support library.

Is it necessary to perform API if-else check when using AppCompat

Previously, I'm using SherlockActionBar library. The following code will work, across Android 2.3 till Android 5.
this.searchMenuItem.collapseActionView();
However, after migrating to AppCompat, we need to migrate to the following code
MenuItemCompat.collapseActionView(JStockFragmentActivity.this.searchMenuItem);
When I look at the documentation http://developer.android.com/guide/topics/ui/actionbar.html#ActionView, it states that
On API level 11 or higher
Get the action view by calling getActionView() on the corresponding
MenuItem:
menu.findItem(R.id.action_search).getActionView()
I was wondering, is it necessary that I need to write my migrated code in the following way?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
this.searchMenuItem.collapseActionView();
} else {
MenuItemCompat.collapseActionView(this.searchMenuItem);
}

image.setAlpha on Android lollipop not work

After testing my app on Android 5.0, I noticed that image.setAlpha() is not working on this Android version.
I tried with image.setImagealpha() function, but it returns this error:
"The method setImageAlpha(int) is undefined for the type Drawable"
The API level that I´m using on my app is 8
What can I do?
Update 2019:
With kotlin now we can do it like this:
imageView.post {imageView.alpha = 1.0f}
I have used View.post so that it get updated on immediate next UI updation cycle.
Without posting on UI thread sometimes setting alpha of TextViews doesn't reflect on UI as discussed here
ImageView has the method setAlpha(float) after API 11. Before API 11 it uses setAlpha(int). Since you want to support API 8 and above, you have to specify the different states. So to resolve this, use the following code:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
//For API 11 and above use a float such as 0.54.(54% transparency)
imageView.setAlpha(float);
}
else
//For API Below 11 use an int such as 54.
imageView.setAlpha(int);

If I set minSdkVersion as 8, but want to specify some methods for SdkVersion=15, how can I do that?

I want to develop an Android application even for android devices with a lower version, while I do not want to sacrifice some available functions in higher versions.
In the AndroidManifest.xml file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
I get the android level of the current device:
deviceLevel = android.os.Build.VERSION.SDK_INT;
Then I will use if statements to specify different code for different versions.
if (deviceLevel >= 11)
{
List<Camera.Size> videoSizes = parameters.getSupportedVideoSizes();
......
}
else
{
......
}
However, Eclipse indicates there exists errors. getSupportedVideoSizes is not availabe for version = 8;
I think I need to suppress the error at this situation.
How to do that?
You will want to add #SupressLint("NewApi") to your activity class. Then you will want to use android.os.Build.VERSION.SDK_INT; to get the version int, which should always be the API number (ie. Jelly Bean 4.2 is API 17, use 17 for your comparison) or you can use Build.VERSION_CODES.DESSERT_NAME_HERE, and use this value for an if-else to perform your action that can only take place in higher level API's.
I think it will be better if you change "minSdkVersion" to 15. Because it will easily die when run at pervious device.

TextView.getAlpha doesnt exist in Android

I want to get current alpha of textview i am using the following code but i am getting an error that
java.lang.NoSuchMethodError: android.widget.TextView.getAlpha
Please guide me.
This method since API Level 11. Check your API version.
View.getAlpha() only exists since API level 11. You are trying to running your code on a too-old version of Android.
If you absolutely require this functionality, then update your app's minSdkVersion in AndroidManifest.xml to prevent it running on older Android versions. If you can live without it, do a runtime check to see if the API level is high enough.
you can use Alpha method like below
Textview tv_password;
tv_password =(TextView) findviewById(R.id.tv1);
tv_password.getBackground().setAlpha(50);
you can't use getAlpha() method with Textview
View.getAlpha() available from API level 11.But if you want to use this on older api then you may use NineOldAndroids library available for using the Honeycomb (Android 3.0) animation API on all versions of the platform back to 1.0!
So use need to change for e.g.
mAlpha = mView.getAlpha();
to
mAlpha = ViewHelper.getAlpha(mView);
where mView is your view.
Note : Don't forget to import com.nineoldandroids.view.ViewHelper;

Categories

Resources