Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Android) Layout won't redraw after setVisibility(view.GONE)?

In app I have:

LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.cvLinearLayout2);

and after:

linearLayout2.setVisibility(View.GONE);

I can't find a way to bring linearLayout2 back.

Tried everything:

  linearLayout2.setVisibility(View.VISIBLE);
  linearLayout2.bringToFront();
  linearLayout2.getParent().requestLayout();
  linearLayout2.forceLayout();
  linearLayout2.requestLayout();
  linearLayout2.invalidate();

but with no results. linearLayout2 have one parent linearLayout1, so I tried also:

  linearLayout1.requestLayout();
  linearLayout1.invalidate();

still with zero results. linearLayout2 stays GONE. In my app I need to move linearLayout away, and then, after a while to redraw it again. Please help.

like image 919
smandic Avatar asked Jan 14 '12 17:01

smandic


2 Answers

View.GONE will remove the View from the screen, and the space occupied by the View is released to other Views on screen. So, a "GONE" view cannot come back. You need to reload it.

If you want to keep the space, you can use View.INVISIBLE. Now the View is NOT removed, instead, it hides the View and display blank space.

In simple illustration, you have the following setup:

ABCD

After calling B.setVisibility(View.INVISIBLE); you will have:

A CD

But after calling B.setVisibility(View.GONE); you will get:

ACD

like image 76
Vishnu Haridas Avatar answered Sep 28 '22 03:09

Vishnu Haridas


Setting a View's visibility to GONE should not affect it's ability to "come back" by using the method setVisibility(View.VISIBLE)

For example I have this code in one of my apps:

public void onCheckedChanged(CompoundButton checkBox, boolean isChecked{
    if(checkBox == usesLocationCheckBox)
    {
        View view = findViewById(R.id.eventLocationOptions);
        if(isChecked)
        {
            view.setVisibility(View.VISIBLE);
            usesTimeCheckBox.setEnabled(false);
        }
        if(!isChecked)
        {
            view.setVisibility(View.GONE);
            usesTimeCheckBox.setEnabled(true);
        }
    }}

And it works perfectly fine. Some other code your program is executing must be responsible. Edit your post with the relevant code, and we may be able to give you a better answer.

like image 25
Garzahd Avatar answered Sep 28 '22 01:09

Garzahd