(Android) The layout will not be redrawn after setVisibility (view.GONE)?

In the application, I have:

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

and after :

 linearLayout2.setVisibility(View.GONE); 

I cannot find a way to return linearLayout2 .

I tried everything:

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

but without results. linearLayout2 have one parent element linearLayout1 , so I also tried:

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

still with zero results. linearLayout2 remains GONE . In my application, I need to move linearLayout , and then, after some time, redraw it again. Please, help.

+4
source share
2 answers

Setting the visibility of the GONE view should not affect the ability to "return" using the setVisibility (View.VISIBLE) method

For example, I have this code in one of my applications:

 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 great. Some other code that your program executes should be held accountable. Edit the message with the appropriate code and we can give you a better answer.

+3
source

View.GONE will remove the view from the screen, and the space occupied by the view will be released into other views on the screen. Thus, the "GONE" view cannot return. You need to restart it.

If you want to keep a space, you can use View.INVISIBLE . Now the View is not deleted, instead, it hides the view and shows empty space.

In a simple illustration, you have the following setting:

ABCD

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

A CD

But after calling B.setVisibility(View.GONE); You'll get:

ACD

+9
source

Source: https://habr.com/ru/post/1391000/


All Articles