0

Im having problems specifiying the height of a relative Layout. From what i understand, these two blocks of code should be equivalent (myLayout is a RelativeLayout that i defined in XML, with an initial height of "0dp", and it's parent is also a RelativeLayout):

BLOCK 1:

RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
p.height = (int)(35*scale);
myLayout.setLayoutParams(p);
myLayout.invalidate();

BLOCK 2:

RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
RelativeLayout.LayoutParams newP = new RelativeLayout.LayoutParams(p.width, (int)(35*scale));
myLayout.setLayoutParams(newP);
myLayout.invalidate();

Scale, in my case, is 2. So i expect myLayout to have a height of 70 after the execution of either of these blocks, and i expect it to return that height when i call myLayout.getHeight(). Also, i expect myLayout to occupy a rect with the height of 70 and its former width (happens to be match_parent).

But when i run the code, the first block does not change the height of myLayout on screen, nor does it change the return value of myLayout.getHeight(). It does, however, change myLayout.getLayoutParams().height.

Now the second block does work, although i have to run it twice(!?) for the change to take effect. Im seriously at a loss here and i cant find anything even closely related to this in the docs.

I thought this would be an easy task when i set out yesterday, but by now im questioning my sanity, among other things. Any Ideas?

katzenhut
  • 1,742
  • 18
  • 26

1 Answers1

1

If size is determined dynamically, you may need to use ViewTreeObserver to get the size seen on the screen.

Set your width and height in defining your params. If you want the width to be match_parent, change your code like below and it is going to work (if you don't want match_parent, just set the width that you desire in the code below):

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                           LayoutParams.MATCH_PARENT, (int)(35*scale));
myLayout.setLayoutParams(params);
canova
  • 3,965
  • 2
  • 22
  • 39
  • Thanks for the answer, im happy to say that it works (and the first time, too ;)). Would you care to elaborate on how this is different from my BLOCK 2? To me it looks like they should be interchangable? ALso, why cant i reuse the old LayoutParams-Object? – katzenhut May 09 '14 at 11:17
  • Glad it is working :) It is different because you get layoutparams and height from it, then try setting that height. This doesn't give you the on screen value because you call it in your onCreate and it is not ready to get. That's why I mentioned using ViewTreeObserver if you want to get the height in your onCreate method. – canova May 09 '14 at 11:25
  • But none of this is in onCreate(). It all happens on the tap of a button after everything is already on screen. – katzenhut May 09 '14 at 11:30
  • isn't your button tap in onCreate, or called in an adapter? Still the same issue. It doesn't update its' value until you call a specific ViewTreeObserver. Purpose of using ViewTreeObserver for this is to get the sizes and changes in your layout but it is not really necessary for your current example. It is needed if you need absolute values (not wrapping or filling). Check from website for its' purpose http://developer.android.com/reference/android/view/ViewTreeObserver.html – canova May 09 '14 at 11:40
  • One more information, just Log your params.width value in your onClick (for your code), and you will see it is not what you want :) if you have match_parent, it returns -1, if your have wrap_content, it returns -2 which are constant values for those attributes. – canova May 09 '14 at 11:43