android - Making all Buttons in a vertical LinearLayout the same width -


i trying make buttons in 1 horizontal linearlayout same width, based on 1 button contains longest text.

what tried go through buttons in layout , setting there size this.

linearlayout layout = (linearlayout) findviewbyid(r.id.buttonlayout);      for(int = 0;i < layout.getchildcount();i++){         view v = layout.getchildat(i);         if(v instanceof button){             if(!(v.getid() == r.id.widestbutton)){                 ((button) v).setwidth(findviewbyid(r.id.widestbutton).getwidth());             }         }     } 

this set buttons size, however, size being set not size of widestbutton, 40% of it.

how make work?

i found answer myself, have found googling.

problem is, calling code on activitys oncreate. @ moment, sizes of buttons not calculated yet.

so found viewtreeobserver, able add listener called when layout done loading. code using is:

final linearlayout layout = (linearlayout) findviewbyid(r.id.buttonlayout);     viewtreeobserver vto = layout.getviewtreeobserver();     vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {         @override         public void ongloballayout() {             layout.getviewtreeobserver().removeongloballayoutlistener(this);             for(int = 0;i < layout.getchildcount();i++){                 view v = layout.getchildat(i);                 if(v instanceof button){                     if(!(v.getid() == r.id.widestbutton)){                         ((button) v).setwidth(findviewbyid(r.id.widestbutton).getwidth());                     }                 }             }         }     }); 

which works fine.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -