Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getHeight() vs getLayoutParams().height

What is the difference between getHeight() and getLayoutParams().height of a View? I have a View (GoogleAdView) and I want to hide it, I set getLayoutParams().height to zero but the ad's height (ad.getHeight()) is not zero.

Is there a way to hide the View so that it doesn't occupy space in the layout?

I've tried to set its visibility to GONE or to set ad.getLayoutParams().height to zero but this doesn't work.

like image 283
Buda Gavril Avatar asked Jan 28 '11 07:01

Buda Gavril


People also ask

How do I find the height and width of RelativeLayout in android programmatically?

private int width, height; RelativeLayout rlParent = (RelativeLayout) findViewById(R. id. rlParent); w = rlParent. getWidth(); h = rlParent.


2 Answers

LayoutParams.height is the height you wish your view will have once laid out and could be set to particular constants like WRAP_CONTENT, getHeight() returns the actual height (it returns 0 until the view isn't laid out). See How Android Draws Views and View - Size, padding and margins.

As Michael said, you have to call requestLayout().

like image 54
bigstones Avatar answered Sep 29 '22 11:09

bigstones


The correct way to hide a view and ignore it in layouts is to use

setVisibility(View.GONE);

If this is not working for you, you need to find out why. Trying to tweak the sizes is not a good path.

If you have problems with your layout, post it here.

like image 28
Michael Avatar answered Sep 29 '22 09:09

Michael