Android - How to use different code lines depending of Android version - android

I have a project which is compatible with Android versions from 10(GINGERBREAD_MR1) to 17(JELLY_BEAN_MR1).
So, I would like to use setBackgroundDrawable for versions lower to 16 and setBackground from version 16 or upper.
I've tried this:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
subMessageFromToLinearLayout.setBackgroundDrawable(null);
} else {
subMessageFromToLinearLayout.setBackground(null);
}
But, Eclipse gives me:
A warning for subMessageFromToLinearLayout.setBackgroundDrawable(null);:
"The method setBackgroundDrawable(Drawable) from the type View is deprecated"
And an error for subMessageFromToLinearLayout.setBackground(null);:
"Call requires API level 16 (current min is 10): android.widget.LinearLayout#setBackground"
How can I fix this errors in order I can use both lines depending of the running Android version?
Thanks in advance.

In general the most robust way makes use of class lazy loading:
static boolean isSDK17()
{
return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
}
if (isSDK17())
xyMode_SDK17.setXyMode(context, mode);
else
xyMode_SDK8.setXyMode(context, mode);
#TargetApi(17)
public class xyMode_SDK17
{
static void setXyMode(Context context, boolean mode)
{...}
}
public class xyMode_SDK8
{
#SuppressWarnings("deprecation")
static void setXyMode(Context context, boolean mode)
{...}
}

Have you seen
ActionBarSherlock gives tons of "Call requires API level 11 (current min is 7)" errors
Android - set layout background programmatically
You can mark it with #TargetApi(16) and #SuppressWarnings("deprecated").
If the error still there, try cleaning the project or restart eclipse.
"ah I know of the .setBackgroundDrawable(Drawable) method but to me the IDE had the same error with api 16 requirement. I am using Eclipse and it seemed to be a bug after reopening the ide and cleaning the code a bit it worked. Than you very much and sorry for trouble".

Related

Android Studio: false Lint warning forcing usage of "Compat" classes when high min SDK (25)

I am working on a project with minSdkVersion set to 25 (aka Android 7.1).
Since this version is quite high, there are a lot of methods I can use without worrying about backward compatibility.
For example, retrieving a drawable, from a Fragment, should be as simple as:
context?.getDrawable(R.drawable.my_drawable)
In the source code, what it does is:
return getResources().getDrawable(id, getTheme());
As far as I am concerned, such a method was introduced in API 21 (Android 5.0).
However, I get the following warning:
Looking at the source code of ContextCompat.getDrawable(...):
if (Build.VERSION.SDK_INT >= 21) {
return context.getDrawable(id);
} else if (Build.VERSION.SDK_INT >= 16) {
return context.getResources().getDrawable(id);
} else { ... }
Since the min SDK is set to 25, the first if will always be called, which then the same code I have written. So why the warning?
I could suppress it with the #SuppressLint("UseCompatLoadingForDrawables") but it kinds of defeat the purpose... or I could follow it...
Is this normal? Should I really use ContextCompat and its affiliates or is there a setting somewhere to remove such a false warning?
PS: the project is also using Android X.
Ran into the same issue. I would say it is a false positive when you have a minSdk >= 21. Since as you say you will always enter the if branch which calls getDrawable.
So suppressing/ignoring it is the way to go until someone can make the lint rule smart enough to detect that you are on minSdkVersion higher than 21. You can ignore it globally by doing this in your build.gradle:
android {
...
lintOptions {
ignore("UseCompatLoadingForDrawables")
}
}
Interestingly context.getColor(R.color.something) does not give a similar warning even though it has similar code in ContextCompat.getColor.

Tool to Check if code contains higher API calls than minimum level

I am working with a large project, which has a minimum API level:16. however, I came across API usages that are above API level 16.
Is there any tool in Android studio or elsewhere, other than testing with a device, to check if the code doesn't violate the minimum required API level or better point it out like an error etc.?
Thank you.
The IDE will use the minimum android SDK, thus you will not get compile errors. If you there are classes in SDK 14 which are moved in sdk 16, yet you are using the imports from SDK 14, it will give a standard compile error.
So no, not that I am aware of.
You can use something like this:
public static boolean supports(final int version) {
return Build.VERSION.SDK_INT >= version;
}
Like this,
if (supports(Build.VERSION_CODES.HONEYCOMB)) {
// do something HONEYCOMB+ compatible here
}
More codes here,
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

#javascriptinterface on Android lower then 4.2 version

I use D3SView library in my app in payment process. It uses #javascriptinterface in one of functions. Can I use it on Android >= 2.3 ? link to required class.
code:
addJavascriptInterface(new D3SJSInterface(), JavaScriptNS);
...
class D3SJSInterface {
D3SJSInterface(){}
#android.webkit.JavascriptInterface
public void processHTML(final String paramString) {
completeAuthorization(paramString);
}
}
How to make this code allowable for >= 2.3 android version?
Can I use it on Android >= 2.3 ?
Yes, though your compileSdkVersion (a.k.a., "build target" in Eclipse) will need to be API Level 17 or higher. On the older devices, the annotation is ignored.

Android build target change makes some code have compile errors. Not sure what is the right approach

I have this code which enables sharing the app:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
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;
}
}
catch ( Exception e )
{
}
return false;
}
private Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"I am using mobile apps for starting a business from http://www.myurl.com");
return shareIntent;
}
// Somewhere in the application.
public void doShare(Intent shareIntent)
{
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
}
catch ( Exception e )
{
}
}
But when I make the build target 2.2 I get a compile error for this code saying this is code level of Android version 14, but my app accepts lower Android versions.
Is there a way not to have this kind of a compile error? Or is there a way around this issue with slightly different code that works more universally?
Thanks!
But when I make the build target 2.2
When you set your build target to API Level 8, you fail to compile, as you are referring to classes that did not exist.
I am going to assume that you are not setting your build target (e.g., Project > Properties > Android in Eclipse) to API Level 8, but instead to something 14 or higher. And I will assume that you really meant that you are setting your android:minSdkVersion to be something like 8.
I get a compile error for this code saying this is code level of Android version 14, but my app accepts lower Android versions.
Correct. That is Lint, telling you that you need to ensure that your code is taking this stuff into account.
Is there a way not to have this kind of a compile error?
Add #TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) to your methods where you are checking the Build.VERSION.SDK_INT and comparing it to Build.VERSION_CODES.ICE_CREAM_SANDWICH.
The #TargetApi() annotation says, in effect, "for the scope of this method, please consider my android:minSdkVersion to be this higher number, not the one normally used". The compile errors will disappear... until such time as you start using something newer than API Level 14 in those methods.

Compiler Ginger Bread, build version codes Jelly Bean Mr1 cannot resolve or is not a field

Question: What code should i use instead of
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ //perform something }
Build.VERSION_CODES.JELLY_BEAN_MR1 error can not be resolved.
If compiled is set to Jelly Bean Mr1 and higher no errors. If compiler is set below Jelly Bean Mr1 errors occur.
minSdk = 9, targetSdk = 19, compiler = 2.3.1(Ginger Bread) see the below screen shot URL
https://www.evernote.com/shard/s283/sh/401b9ed5-d51d-4d55-b23d-6ebe8eeb8d03/212a791182b162df8dadb614445f2d2d
if Build.VERSION_CODES.JELLY_BEAN_MR1 gives a problem, only write:
import android.os.Build;
if you don't want to write it you can use
if( Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
{
//perform something
} else {
// perform other actions for other versions (below than jelly..)
// find another way to do what you want.
}
if the problem persist maybe you don't have the necessary libraries, please update your libraries with Android SDK Manager.
JELLY_BEAN_MR1 ~ Android 4.2
You should install and use Android 4.2 or higher as a compiler.

Categories

Resources