Sunday, February 27, 2011

how to get width and height dimensions of a customView (extends View) in Android

So what I was trying to do is get the following dimensions of ONE out of the TWO framelayouts that I had put on my android application.

As obvious as it is, I went to the Android Developer's website to look up the available methods for finding width and height. And it said android.view.View.getWidth() and android.view.View.getHeight() will do just the thing. But..... it did NOT do the thing that way.

I had been looking around all over the forums and sites to find the solution.... LOTs of people are asking this questions, but i couldn't find that absolute solution from any of the forums. So, once I figured out the way, I thought of putting the solution up on my blog.

At first, here is how the android activity looks:

Activity with a Toast Message showing
width and height of the green frame
Now, here is the xml layout file that was used in this example


  1. You made my day! Thanks alot!

  2. Cheers mate, but Christ! I want to love Android development but it's SO painful, a simple thing like measuring the size of a View requires a custom class!?

  3. totally agree with you on that one........ stupid stuff.

  4. thank you man.....i was trying this since 2 days....

  5. Thanx a lot. You made my day.

    onSizeChanged() solution works in my case to get Height and width of a custom view.

  6. Syed, Compiled an ran the example. The following code has no effect that can be seen. Anyone else have this problem?. Thanks, Darin.

    protected void onDraw(Canvas canvas){

    String msg = "width: " + viewWidth + "height: " + viewHeight;

    1. System.out.println(msg) cannot be seen on the emulator screen. It can be seen in the logcat. You could instead produce a Toast. The toast would show up on the emulator's screen.

      The trick to determine the width and/or height happens in the onSizeChanged() method. The onDtaw() method (in this example) just USES the determinED width and height.

      Hope i could help.

  7. Thanks again Syed I have all the scaling code down all I need now is how to get the intial height to work with for a livewallpaper, if you want I can email you what I got and maybe together we can come up with a solution for this, the way I got set up it will scale  all images and also were they should be placed, only one thing is needed and that is how to get that intial height let me know if you want to try this I can email the files maybe we both can learn this and get it going, I know the rescaling in android is a nightmare for a lot folks, this method I have will even scale the x and y positions properly but I am just learning myself

  8. thanks for the feedback.... i do not seem to have much experience with live wallpapers... you can try this though if it helps...

    new LayoutParams(
    getWindowManager().getDefaultDisplay().getWidth() / myListOfViews.length,
    getWindowManager().getDefaultDisplay().getHeight() / 2

    what this does is positions a row of views (eg. buttons) on the screen whose heights are half of that of
    the screen size....... and whose widths are the width of the screen equally divided among the set of views.

    i am not sure if my answer helped you in your quest.... i am also a newbie on android... has a REALLY good community of experienced and newcomer android developers.... try tapping in there.


  9. excellent tutorial, I am just starting to learn java and android and I have been looking for a way to get the width and height of the screen then use that to rescale all images and positions of images by creating a ratio from the original height * background image height (which will always be bigger, this is for a livewallpaper I am trying to create, but I just am having issues passing the information to other class files, hopefully I can figure this one out.
    Unless you have a way of doing this, like I said I am learning and it's probably a simple solution.
    Thanks for this I will try it.

  10. Thanks for this post, you helped me out.

  11. Finally, someone spells this out clearly. Thank you!

  12. thankyou for help

  13. Thanks, just what i was looking for!

  14. Thanks for a good post on android application. I have subscribed to your RSS feed.


  15. awesum example dude...simple and TO THE POINT :-)...thnx...

  16. how you get Height?

  17. Хильмек ру7:26 PM, October 05, 2012

    Thank you so much! That solved all problems I had in my app! :)