I used the setLayerType method in my app to check the device hardwareAccelerated true or false, it's working on higher version (i.e 3+) but in lower version the app is crashing.
Here is my code snippet:
try {
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD) {
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
} catch(Exception ex) {
}
I tried to check on AndroidManifest.xml but that doesn't work for me.
Api level check you have set is wrong. Change to
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
The setLayerType() works only on Api 11 and above. And GINGERBREAD is Api 9 and GINGERBREAD_MR1 is Api 10.
Related
I have this code:
public void run() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// This action will have the side-effect of blurring the currently focused element
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
}
But the id KITKAT gets the error: KITKAT cannot be resolved or is not a field
Build.VERSION_CODES.KITKAT
As in docs : October 2013: Android 4.4, KitKat, another tasty treat.
means KITKAT constant added in API level 19.
So change current project target version to API level 19(Android 4.4)
I'm not new to Android and I'm well used to the version handling and how to condition it, but when I see this it troubles me...
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
On any device pre lollipop this line would crash the app because the Build.VERSION_CODES.LOLLIPOP field does not exists... so why is this in the recommended solution in the documentation?
I'm really wondering what am I missing?
Well in that case use this
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
Build.VERSION_CODES.LOLLIPOP = 21
Well, you must compile your project with the latest SDK version. Your constants are replaced with corresponding integer values during compilation. No matter what version of Android you run the application on - integers are the same
Try this one
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
Note: Build.VERSION_CODES.LOLLIPOP_MR1==22
Build.VERSION_CODES.M==23
Bit a late to answer, but, today I encountered with the same issue on Android Studio 3.4.1
So the workaround is:
Upgrade to the latest Android SDK.
And,
After Marshmallow/Android 6 Build.VERSION_CODES.xxx full names are replaced with initials and some other variations.
So, now for Marshmallow, it will be:
Build.VERSION_CODES.M
And for Nougat:
Build.VERSION_CODES.N
And so on.
Read more about the build version codes here: Android Developer Reference
I want to launch NFC settings activity, which is done using action ACTION_WIRELESS_SETTINGS prior to API level 16. But in API level 16 and above it is done using ACTION_NFC_SETTINGS.
I am compiling my source using Android 4.0.3 which is API level 15.
How can I support for higher level so that it can open NFC settings in all versions.
Do i need to compile my source with API level 16 or higher and make min sdk version 15?
You would typically compile your application with the SDK of the highest API version that you use in your code (or the latest available SDK). In your app's manifest you add the minimum and target SDK versions:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="..." />
These values define how your app will be treated on a device in terms of (backwards) compatibility features.
Then, in your code, whenever you use API calles that are not available on a certain API level, you would surround those calles with a check for the Build.VERSION.SDK_INT (as JaKoZo showed).
With regard to the NFC settings, you could do even without that check and instead catch the exception that is thrown when no activity is registered for the ACTION_NFC_SETTINGS intent:
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_NFC_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e) {
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e1) {
}
}
Instead of the constant ACTION_NFC_SETTINGS, you could also use the hard-coded string "android.settings.NFC_SETTINGS". While I don't really recommend this, you would not need to build against a higher API level (if it's only that constant that causes you to do so).
[...]
this.startActivityForResult(
new Intent("android.settings.NFC_SETTINGS"),
1); // magic number to detect when this startActivityForResult returns
[...]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//do stuff
}
u mean this?
in my app minSdkVersion="16" and targetSdkVersion="19". And I wish to use MediaMuxer that introduced in API 18. So when I detetect the API on the phone how can I deside if API>=18 use
MediaMuxer and if API<18 use other function. When targeting minSdkVersion="16" the function MediaMuxer is now shown at all in eclipse.
Test the version number like this
if (Build.VERSION.SDK_INT >= 18) {
// do high api
else {
// do low api
}
You have to check the target version of the Android device using below code
if (android.os.Build.VERSION.SDK_INT >= 16) {
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}else{
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}
This question already has answers here:
setBackground vs setBackgroundDrawable (Android)
(12 answers)
Closed 8 years ago.
I wanted to use view.setBackgroundDrawable(Drawable) but this method is deprecated. It is replaced with .setBackground(Drawable). But my minimum of API 8 can't handle that. It tells me to set the minimum to API 16.
Is there a way to use a different method, based on the API of the device?
Something like
if(API<16)
{
view.setBackgroundDrawable(Drawable)
}
else
{
view.setBackground(Drawable)
}
Or do I really have to change the minimum API to do this?
setBackgroundDrawable is deprecated but it still works so you could just use it. But if you want to be completely correct you should use something like this
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable()
} else {
setBackground();
}
For this to work you need to set buildTarget api 16 and min build to 7 or something similar.
Something like this:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
view.setBackgroundDrawable(Drawable)
} else {
view.setBackground(Drawable)
}
You can use different methods based on the API versions.
For e.g:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
//Methods for version <8 (FROYO)
} else {
// Methods for version >=8
}
Here set your targetSDkversion to any higher versions(for e.g 16 here) and set your minsdkversion to lower versions ( API 7).