I am trying to apply an animation to a view in my Android app after my activity is created. To do this, I need to determine the current size of the view, and then set up an animation to scale from the current size to the new size. This part must be done at runtime, since the view scales to different sizes depending on input from the user. My layout is defined in XML.
This seems like an easy task, and there are lots of SO questions regarding this though none which solved my problem, obviously. So perhaps I am missing something obvious. I get a handle to my view by:
ImageView myView = (ImageView) getWindow().findViewById(R.id.MyViewID);
This works fine, but when calling getWidth()
, getHeight()
, getMeasuredWidth()
, getLayoutParams().width
, etc., they all return 0. I have also tried manually calling measure()
on the view followed by a call to getMeasuredWidth()
, but that has no effect.
I have tried calling these methods and inspecting the object in the debugger in my activity's onCreate()
and in onPostCreate()
. How can I figure out the exact dimensions of this view at runtime?
match_parent
). In this case, a simpler solution that can safely be called even before the View is drawn (e.g., from your Activity's onCreate() method) is just to use Display.getSize(); see stackoverflow.com/a/30929599/5025060 for details. I know this isn't what @NikReiman asked for, just leaving a note to those who may be able to use this simpler method. – CODE-REaD Aug 16 '16 at 13:37