Search code examples
androidandroid-linearlayout

(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.


Solution

  • 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.