Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I annotate my method to return a Visibility type int in Android? [duplicate]

Tags:

android

Android studio complains when I return a regular int and try to use that int as an input parameter to a Visibility-annotated int. How can I annotate my method so that AS doesn't produce this warning? I see that View has a public @interface "Visibility" but for some weird reason I can't reference android.view.View.Visibility from my project even if it's public. Why?

My method:

public static int booleanToVisibleOrInvisible(boolean visible) {
    return visible ? View.VISIBLE :  View.INVISIBLE;
}

What I need:

@android.view.View.Visibility  // <- does not compile
public static int booleanToVisibleOrInvisible(boolean visible) {
    return visible ? View.VISIBLE :  View.INVISIBLE;
}
like image 491
Nilzor Avatar asked Jul 28 '15 08:07

Nilzor


1 Answers

but for some weird reason I can't reference android.view.View.Visibility from my project even if it's public.

Take a look into the source code, you'll see that this annotation is 'annotated' with the @hide tag:

/** @hide */
@IntDef({VISIBLE, INVISIBLE, GONE})
@Retention(RetentionPolicy.SOURCE)
public @interface Visibility {}

@hide tag is used to exclude the annotated package/class/method/etc from the android.jar file so it's not available at compile time. I doubt you can somehow use this same annotation in your code unless Google engineers remove the @hide tag from the sources.
You can suppress this warning with //noinspection ResourceType comment.

like image 130
aga Avatar answered Sep 28 '22 02:09

aga