1

I'm new on Android development, so I'm developing an simple application that hides a textview pressing some button, so in the java code in the method for the OnClick event for the button I set the textview as invisible, I used:

textView.setVisibility(1);

because 1 is the value for "invisible" described in the android reference, but it does not work, so after I used

textView.setVisibility(View.INVISIBLE);

and it works, so When is the "1" value used? and Why is View.INVISIBLE = 4 instead of 1 as the android reference says?

In the android reference I can see that the value Invisible for attribute android:visibility is defined as 1

4
  • The reference you posted a link to clearly shows that View.INVISIBLE is 4. Commented Feb 18, 2013 at 2:53
  • Maybe he is wondering why there is a inconsistency between R.attr and the constant. Commented Feb 18, 2013 at 2:57
  • Exactly, I can't figure it out why android:visibility is 1 and View.INVISIBLE is 4 Commented Feb 18, 2013 at 2:59
  • yeah, maybe it's some inconsistency Commented Feb 18, 2013 at 3:00

2 Answers 2

3

Both are difference. According to Android Reference

textView.setVisibility(1);

is same with

textView.setVisibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);

If you want to hide the view, use :

textView.setVisibility(View.INVISIBLE);

Or

textView.setVisibility(4);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering, but why in the android reference the value "invisible" for android:visibility is defined as 1?
2

This is a good question, I checked the Android source code (frameworks/base/core/java/android/view/View.java)

case com.android.internal.R.styleable.View_visibility:
    final int visibility = a.getInt(attr, 0);
    if (visibility != 0) {
          viewFlagValues |= VISIBILITY_FLAGS[visibility]; //here is the key to your question
          viewFlagMasks |= VISIBILITY_MASK;
    }
    break;

Here is the content of VISIBILITY_FLAGS:

private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};

The value of the array elements are actually the value shown in Android Reference

/**
     * This view is visible.
     * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
     * android:visibility}.
     */
    public static final int VISIBLE = 0x00000000;

    /**
     * This view is invisible, but it still takes up space for layout purposes.
     * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
     * android:visibility}.
     */
    public static final int INVISIBLE = 0x00000004;

    /**
     * This view is invisible, and it doesn't take any space for layout
     * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
     * android:visibility}.
     */
    public static final int GONE = 0x00000008;

So even if you use the android:invisible in the manifest file, Android framework will finally call setVisibility(...) with the value 4.

1 Comment

Ok thanks, it's good to know the code, I can't find where that "1" for the main.xml is used for, in the .xml you can't set 1 as value, you have to set "invisible", and neither in the activity....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.