Furqan Ali
Furqan Ali

Reputation: 137

Get Height of LinearLayout or RelativeLayout in Android programmatically

I need to get the length of LinearLayout or RelativeLayout programmatically in Android like if I have a LinearLayout as follows

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" > 

I need to increase height of the layout which is located in the mid of the screen by taking screen height and increase or decrease its height by taking calculations on the basis of screen height. So is there is a way of calculating the height of a Layout whose height is set as wrap_content. I tried to get Height from Layout Parameters but it gives me the value of -1 or -2 not height in pixels or density pixels.

Upvotes: 0

Views: 402

Answers (2)

elmorabea
elmorabea

Reputation: 3263

Try the following:

myLayoutRef.post(new Runnable() {
    int height = myLayoutRef.getHeight;
});

This runnable runs after your layout has been drawn and laid out on screen, so get height returns the real height and not 0

Upvotes: 1

QArea
QArea

Reputation: 4981

Of course match_parent and wrap_content equals like -1 and -2.

In this case try to use viewTreeObserver.

Here a good link: http://www.sherif.mobi/2013/01/how-to-get-widthheight-of-view.html

Upvotes: 2

Related Questions