2

I need to change the image in an imageview then get the width. Problem is, it gives me the width of the last image.

headpic.setImageResource(head[x]);
int width = headpic.getWidth();

if i then run this code again with x being the same value it gets the correct width.

Steven Johnston
  • 142
  • 1
  • 11
  • headpic probably isn't updated yet when you use the .getWidth() method. – Igor Aug 13 '13 at 18:50
  • yea thats what i was thinking its not in onCreate() it in a method from a button. – Steven Johnston Aug 13 '13 at 18:59
  • You probably need the OnPreDraw event. Check here: http://stackoverflow.com/questions/4649438/how-to-get-the-dimensions-of-a-drawable-in-an-imageview/4649842 – Igor Aug 13 '13 at 19:02

1 Answers1

1

I'm assuming headpic is your imageview. If so, wouldn't headpic.getWidth(); be getting the width of the imageview instead of the image?

You could get the drawable from the imageview and use .getIntrinsicWidth();

ZhaoYiLi
  • 173
  • 1
  • 10