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

19 comments:

  1. You made my day! Thanks alot!

    ReplyDelete
  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!?

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

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

    ReplyDelete
  5. Thanx a lot. You made my day.

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

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

    @Override
    protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    String msg = "width: " + viewWidth + "height: " + viewHeight;
    System.out.println(msg);
    }

    ReplyDelete
    Replies
    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.

      Delete
  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
    Sam

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

    myListOfViews.get(i).setLayoutParams(
    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... stackoverflow.com has a REALLY good community of experienced and newcomer android developers.... try tapping in there.

    tc

    ReplyDelete
  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.
    Sam

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

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

    ReplyDelete
  12. thankyou for help

    ReplyDelete
  13. Thanks, just what i was looking for!

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

    Maneesh
    Gotocamera.com

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

    ReplyDelete
  16. how you get Height?

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

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

    ReplyDelete