Take the 2-minute tour ×

I want to get a layout's width in pixels. I tried this:

LinearLayout galleryContainer = (LinearLayout)findViewById(R.id.galleryContainerLayout);
int galleryWidth= galleryContainer.getLayoutParams().width;

if i toast out galleryWidth, it is says "-1".

I also tried


but it is gives 0 at the first time, then 480 (the actual size) at second time, so it is like i got the pixels BEFORE my layout infiltrate its size.

What the hell?

How can i really get a layouts width in pixels ? Or some exact values like dip?

share|improve this question
add comment

3 Answers

Use measure() before call getWidth()

galleryContainer.measure(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
share|improve this answer
getting crash without excepetion with this code. What are the .FILL_PARENT tags for ? My galleryContainer doesnt have fill_parent width and heigths. Both of them are match_parent. –  Adam Varhegyi Apr 18 '12 at 11:42
there are three ways to grab outfit in screen for every widget:: FILL_PARENT = to grab the full available screen, WRAP_CONTENT = to garb the required screen for the current widget, Manual Size e.g. "10dp"/"10px" = as per your choice. Hope you get what i mean. –  Harpreet Apr 23 '12 at 12:31
add comment

Not sure for you but we can code something like this too, check out if it helps.

                        int width = display.getWidth();
                        int height = display.getHeight();

Have fun.

share|improve this answer
add comment

The layout is inflated but not yet drawn.In order to get width or height, you will have to use the below method.

public void onWindowFocusChanged(boolean hasFocus) 
     // TODO Auto-generated method stub 
share|improve this answer
The method onWindowFocusChanged(boolean) of type ShopActivity.SendRating must override or implement a supertype method Why im getting this error if it is already overrided ? The more stranger, that eclipse suggests me to do this: 1 quick fix: -Remove override annotation. What the..? –  Adam Varhegyi Apr 18 '12 at 11:44
r u using this method in a activity or non-activity class?. This problem is likely to occur if your java compliance is 1.6 or low. You have to change it to 1.7. or try to remove the override annotation and check. –  Andro Selva Apr 18 '12 at 12:06
add comment

Your Answer


