Determining the size of an Android view at runtime -


i trying apply animation view in android app after activity created. this, need determine current size of view, , set animation scale current size new size. part must done @ runtime, since view scales different sizes depending on input user. layout defined in xml.

this seems easy task, , there lots of questions regarding though none solved problem, obviously. perhaps missing obvious. handle view by:

imageview myview = (imageview)getwindow().findviewbyid(r.id.myviewid); 

this works fine, when calling getwidth(), getheight(), getmeasuredwidth(), getlayoutparams().width, etc., return 0. have tried manually calling measure() on view followed call getmeasuredwidth(), has no effect.

i have tried calling these methods , inspecting object in debugger in activity's oncreate() , in onpostcreate(). how can figure out exact dimensions of view @ runtime?

use viewtreeobserver on view wait first layout. after first layout getwidth()/getheight()/getmeasuredwidth()/getmeasuredheight() work.

viewtreeobserver viewtreeobserver = view.getviewtreeobserver(); if (viewtreeobserver.isalive()) {   viewtreeobserver.addongloballayoutlistener(new ongloballayoutlistener() {     @override     public void ongloballayout() {       view.getviewtreeobserver().removeongloballayoutlistener(this);       viewwidth = view.getwidth();       viewheight = view.getheight();     }   }); } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -