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

293,058

Solution 1

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);
}

Solution 2

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

myViewGroup.setVisibility(View.GONE);

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

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

Placing breakpoints you can 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 to run the animation.

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

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

Solution 3

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>

Solution 4

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

Solution 5

In my case I found that simply clearing the animation on the view before setting the visibility to GONE works.

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

I had a similar issue where I toggle between two views, one of which must always start off as GONE - But when I displayed the views again, it was displaying over the first view even if setVisibility(GONE) was called. Clearing the animation before setting the view to GONE worked.

Share:
293,058
nayden
Author by

nayden

Updated on July 05, 2022

Comments

  • nayden
    nayden almost 2 years

    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.

    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());
        }
    });
    
  • nayden
    nayden almost 13 years
    the text can be invisible but the button and datepicker no.
  • nayden
    nayden almost 13 years
    ArrayAdapter<String> adapter7 = new ArrayAdapter<String>(Application.getContext(), android.R.layout.simple_spinner_item, options()); Spinner spinnerEnd = (Spinner) findViewById(R.id.spinnerEnd); adapter7.setDropDownViewResource(android.R.layout.simple_spi‌​nner_dropdown_item); spinnerEnd.setAdapter(adapter7); spinnerEnd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { dp2.setVisibility(View.VISIBLE); btn2.setVisibility(View.VISIBLE);
  • nayden
    nayden almost 13 years
    the problem is from here. Becouse go inside of onItemSelect
  • Lukap
    Lukap almost 13 years
    DO NOT USE literal integers , that is bad habit, use View.GONE or View.VISIBLE instead...
  • Parag Chauhan
    Parag Chauhan almost 13 years
    Try this Button btn1,btn2; Spinner spin; btn1=(Button)findViewById(R.id.button1); spin=(Spinner)findViewById(R.id.spinner1); String[] sample11={"Sample","HI"}; spin=(Spinner)findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,sample11); spin.setAdapter(adapter); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub spin.setVisibility(View.GONE); } });
  • Maxime Claude
    Maxime Claude about 7 years
    This is the answer for me ( View.clearAnimation()). Always call View.clearAnimation() on a view you want to set to GONE when you know that you set animations on it.
  • Rainmaker
    Rainmaker over 5 years
    this is really valid, debugging didn't shed any light as the visibility was indeed set as expected!
  • rHd
    rHd over 3 years
    Thanks! I spent a lot of time figuring why the visibility is not working and your solution worked for me!
  • Amin
    Amin almost 3 years
    yes, so I wanna check with IF CONDITION but I can't do it.
  • AtomicallyBeyond
    AtomicallyBeyond over 2 years
    It doesn't work for me, since I'm using androidx.constraintlayout.motion.widget.MotionLayout as container for the view.