0

I'm trying to return the width and height of my ImageView but keep being returned 0. I'm calling ImageView.getWidth() and ImageView.getHeight() in OnCreate() and I believe the ImageView hasn't had enough time to initialize, thus the error I'm getting:

java.lang.IllegalArgumentException: width and height must be > 0

My question is: Where is a good place to retrieve the width and height of my ImageView so I know it has enough time to be initialized properly?

Note: I've tried onWindowFocusChanged() but get the same issue.

Cheers!

LKB
  • 1,020
  • 5
  • 22
  • 46
  • 1
    possible duplicate of [android get height width of ImageView](http://stackoverflow.com/questions/4680499/android-get-height-width-of-imageview) – Ryan S Jul 04 '13 at 23:59
  • Hi - the answer is here: [android-get-width-returns-0](http://stackoverflow.com/questions/3591784/android-get-width-returns-0). One option: "you can use a dimension resource to define the button size, then reference that dimension resource from your layout file" – paulsm4 Jul 05 '13 at 00:00

1 Answers1

3

Quote from "Hello Android (Third Edition)" page 81:

A common mistake made by new Android developers is to use the width and height of a view inside its constructor. When a view’s constructor is called, Android doesn’t know yet how big the view will be, so the sizes are set to zero. The real sizes are calculated during the layout stage, which occurs after construction but before anything is drawn. You can use the onSizeChanged( ) method to be notified of the values when they are known, or you can use the getWidth( ) and getHeight( ) meth- ods later, such as in the onDraw( ) method.

Kaifei
  • 1,568
  • 2
  • 13
  • 16