hsz
hsz

Reputation: 152216

Change LinearLayout height to 0 programmatically

I am resizign LinearLayout from it's original height to 0 with:

ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
requestLayout();

Everything works except newHeight = 0 - layout's height changes back to its original height. How can I avoid it ?

Setting visibility to GONE if newHeight == 0 does not help.

Upvotes: 5

Views: 18941

Answers (5)

murat ertürk
murat ertürk

Reputation: 31

layout = (LinearLayout)findViewById(R.id.linearTop);

layout.getLayoutParams().height =  ViewGroup.LayoutParams.WRAP_CONTENT;                
ViewGroup.LayoutParams params =    layout.getLayoutParams();
params.height =  ViewGroup.LayoutParams.WRAP_CONTENT;
layout.setLayoutParams(params);

Upvotes: 3

blessanm86
blessanm86

Reputation: 31779

Try setting a new layout params object then. This should work

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,0);
layout.setLayoutParams(lp);

Upvotes: 0

Mehul Santoki
Mehul Santoki

Reputation: 1208

Try this.....

 LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);

 LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
 lp.height = 0;

Upvotes: 13

Kirill Gamazkov
Kirill Gamazkov

Reputation: 3397

Are you sure that you get the actual parameters object, not the copy of one? I would try the following code:

ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
setLayoutParams(params);
requestLayout();

Upvotes: 0

Ridcully
Ridcully

Reputation: 23655

Wouldn't you have to set the height of params using setBaseAttributes()? The int height = ... will not have any influence on params...

Upvotes: 0

Related Questions