'Call requires API level 23' error, but getForeground() exists on FrameLayout from API 1
Asked Answered
A

1

29

Summary

Finally, I found the related issue, setForeground() incorrectly flagged as requiring API 23 on ViewGroups extending FrameLayout.

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) for ViewGroups extending FrameLayout

If you are not extending FrameLayout, the documentation is wrong and API 23 is required.

Issue 186273: View.setForeground wrong API level

Updated question

Marked as duplicated of Can't use setForeground method on ImageView by Commonsware and answered by him:

That is a documentation bug. setForeground() existed on FrameLayout from API Level 1; it is only on View as of API Level 23.

In my case the documentation bug is not detected by the inspection in the fork but does it in an integration project, I don't understand it, anyway, this answers my initial question.

But It's not the same case, this code is already extending a Framelayout and using this method, it's not an ImageView, so I suppose the method has been removed in API 23. It doesn't appear in the reference now.

Update: I add a diff report for FrameLayout:

enter image description here

New question

So my question changes to how to use the method in previous versions, and, why does appear the inspection error selectively?

If I add a redundant cast, the error message disappears:

((FrameLayout) layout).getForeground()

But it's accessing the View method that is not supported now.

So I suppose targeting a previous API would remove the error message but I would appreciate a solution to use this Framelayout method or a clue to fix the issue in my case:

ANDROID_COMPILE_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=23.0.1
ANDROID_DEFAULT_MIN_SDK_VERSION=19
ANDROID_DEFAULT_TARGET_SDK_VERSION=23

Workaround

In short, as a workaround, I changed the type to FrameLayout directly, so warnings about API and casting are gone, and I tested it works on Android 4.4.4 device and debugging, I thought it would crash, anyway it's only used in one animation, and I will fix/replace it in the future.

Initial question

I forked and customised the Google sample android-topeka, and android-studio shows an error that I don't understand about the API level required to use the method getForeground() of the class View in android.view package:

Call requires API level 23 (current min is 19): android.view.View#getForeground

The reference marks the method as added in API level 1, and the documentation (Control+Q) shows the same:

Doc showing API level 1 method

But the Calling new methods on older versions inspection shows an error that API level 23 is required:

Error message about API 23 required

I found a similar question, a bug in that case, but I think this is not a bug due to the original fork doesn't show this error message.

I'll try to detect the first time the message appears, but I did a lot of changes so any clue would be appreciated, thanks.

Aprilette answered 27/10, 2015 at 22:23 Comment(0)
A
21

Finally I found the related issue, setForeground() incorrectly flagged as requiring API 23 for ViewGroups extending FrameLayout.

Issue 189041: setForeground() incorrectly flagged as requiring API 23 (NewApi) for ViewGroups extending FrameLayout

If you are not extending FrameLayout, the documentation is wrong and API 23 is really required.

Issue 186273: View.setForeground wrong API level

Aprilette answered 28/10, 2015 at 8:42 Comment(4)
So did you figure out how can we get or set foreground for devices below API 23?Unbrace
If you are extending a FrameLayout, you can use a redundant cast or change the type to FrameLayout to avoid the documentation bug. Otherwise, it's not possible, but maybe the next workaround is useful for you https://mcmap.net/q/394789/-can-39-t-use-setforeground-method-on-imageview-duplicateAprilette
check my answer here as a workaroundStiletto
Thanks, nice, related answers here as wellAprilette

© 2022 - 2024 — McMap. All rights reserved.