Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

I want my DatePicker and the button to be invisible in the begining. And when I press my magic button I want to setVisibility(View.Visible);

The problem here is when I setVisibility(View.GONE) or setVisibility(View.INVISIBLE) nothing changes and the component is still visible.

  • Android material aligning font baseline to 4dp grid
  • OpenID for android apps that require SignIn
  • Getting Year, Month and Date in Android
  • How to print to the console in Android Studio?
  • Trying to work with ExoPlayer - lots of unknowns
  • Moving and vanishing lines of code; trouble with Eclipse's XML Editor
  • final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
    final Button btn2 = (Button) findViewById(R.id.btnDate2);
    
    dp2.setVisibility(View.GONE);
    dp2.setVisibility(View.INVISIBLE);
    btn2.setVisibility(View.GONE);
    btn2.setVisibility(View.INVISIBLE);
    
    btn2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            TextView txt2 = (TextView) findViewById(R.id.txt2);
            txt2.setText("You selected " + dp2.getDayOfMonth()
                + "/" + (dp2.getMonth() + 1) + "/" + dp2.getYear());
        }
    });
    

    Related posts:

    How can I find the maximum texture size for different phones?
    Partial-screen WebView with <video> cut in half
    Run time exceptions in android studio start Indexing time
    Where to put google-services.json file into eclipse project?
    How to check Android Asset resource?
    Android Drag and Drop getClipData returns always null
  • RippleDrawable created programmatically looks different from its xml counterpart
  • Best way to reuse a footer menu in Android
  • Computer Vision and AR libraries availabe for Android?
  • Using ECC Curve25519 to encrypt/decrypt data in Java
  • notifydataSetChanged on Adapter will update with new items, but will not update the existing items
  • How to enforce the custom permission on an Activity in Android?
  • 6 Solutions collect form web for “Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work”

    I see quite a few things wrong. For starters, you don’t have your magic button defined and there is no event handler for it.

    Also you shouldn’t use:

    dp2.setVisibility(View.GONE);
    dp2.setVisibility(View.INVISIBLE); 
    

    Use only one of the two. From Android documentation:

    View.GONE This view is invisible, and it doesn’t take any space for
    layout purposes.

    View.INVISIBLE This view is invisible, but it still
    takes up space for layout purposes.

    In your example, you are overriding the View.GONE assignment with the View.INVISIBLE one.


    Try replacing:

    final DatePicker dp2 = new DatePicker(this)
    

    with:

    DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);  
    

    Similarly for other widgets:

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
    
            final DatePicker dp2 = new DatePicker(this);
            final Button btn2 = new Button(this);
            final Button magicButton = new Button(this);
            final TextView txt2 = new TextView(TestActivity.this);
    
            dp2.setVisibility(View.GONE);
            btn2.setVisibility(View.GONE);
            btn2.setText("set Date");
    
            btn2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    txt2.setText("You selected "
                        + dp2.getDayOfMonth() + "/" + (dp2.getMonth() + 1) 
                        + "/" + dp2.getYear());
                }
            });
    
            magicButton.setText("Magic Button");
            magicButton.setOnClickListener(new View.OnClickListener()    
                public void onClick(View arg0) {
                    dp2.setVisibility(View.VISIBLE);
                    btn2.setVisibility(View.VISIBLE);
                }
            });
    
        ll.addView(dp2);
        ll.addView(btn2);
        ll.addView(magicButton);
        ll.addView(txt2);
    
        setContentView(ll);
    }
    

    You can think it as a CSS style visibility & display.

    <div style="visibility:visible; display:block">
        This is View.VISIBLE : Content is displayed normally.
    </div>
    
    <div style="visibility:hidden; display:block">
        This is View.INVISIBLE : Content is not displayed, but div still takes up place, but empty.
    </div>
    
    <div style="display:none">
        This is View.GONE : Container div is not shown, you can say the content is not displayed.
    </div>
    

    First see your code:

    dp2.setVisibility(View.GONE);
    dp2.setVisibility(View.INVISIBLE);
    btn2.setVisibility(View.GONE);
    btn2.setVisibility(View.INVISIBLE);
    

    Here you set both visibility to same field so that’s the problem.
    I give one sample for that sample demo

    View.GONE This view is invisible, and it doesn’t take any space for layout purposes.

    View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

    dp2.setVisibility(View.GONE);
    dp2.setVisibility(View.INVISIBLE);
    btn2.setVisibility(View.GONE);
    btn2.setVisibility(View.INVISIBLE);
    

    Today I had a scenario, where I was performing following action:

    myViewGroup.setVisibility(View.GONE);
    

    Right on the next frame I was performing an if check somewhere else for visibility state of that view. And guess what? The following condition was passing:

    if(myViewGroup.getVisibility() == View.VISIBLE) {
        // this if check was fulfilled magically
    }
    

    Placing breakpoints you see, that visibility changes to GONE, but right on the next frame it magically becomes VISIBLE. I was trying to understand how the hell this could happen.

    Turns out there was an animation applied to this view, which internally caused the view to change it’s visibility to VISIBLE until finishing the animation:

    public void someFunction() {
        ...
        TransitionManager.beginDelayedTransition(myViewGroup);
        ...
    
        myViewGroup.setVisibility(View.GONE);
    }
    

    If you debug, you’ll see that myViewGroup indeed changes its visibility to GONE, but right on the next frame it would again become visible in order the animation to run.

    So, if you come across with such a situation, make sure you are not performing an if check in midst of animating the view.

    You can remove all animations on the view via View.clearAnimation().

    Because you set visibility either true or false.
    try that
    setVisible(0) to visible true .
    and setVisible(4) to visible false.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.