android - Getting calculated width of a button with Java (returns -2?) -


i want set width , height of progress circle height of item next it, button. in end want have circle equal height , width, occupies total height of search button - if there's easier way that, please tell me.

i guess cannot done using simple xml, i'm trying use layoutparams this, in oncreate() function of activity:

layoutparams lpe = search.getlayoutparams(); layoutparams lpp = searchprogress.getlayoutparams(); lpp.width = lpe.height; lpp.height = lpp.width; toast.maketext(getbasecontext(),"width: "+lpp.width,toast.length_long).show(); toast.maketext(getbasecontext(),"height: "+lpp.height,toast.length_long).show(); searchprogress.setlayoutparams(lpp); 

this doesn't work width, work height. added toasts see debugging info. both lpp.width , lpp.height -2. mean? here's xml:

<button     android:id="@+id/searchbutton"     style="?android:attr/buttonstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignbottom="@+id/searchprogress"     android:layout_aligntop="@+id/searchprogress"     android:layout_toleftof="@+id/searchprogress"     android:text="@string/searchbutton" />  <progressbar     android:id="@+id/searchprogress"     style="?android:attr/progressbarstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignbottom="@+id/searchbar"     android:layout_alignright="@+id/searchresult"     android:layout_aligntop="@+id/searchbar"     android:indeterminate="true"     android:indeterminatebehavior="repeat" /> 

according this -2 means wrap_content. don't want have height in xml, can see myself. want height calculated device.

how can work?

you can try this

public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      final button searchbutton = (button) findviewbyid(r.id.searchbutton);     final progressbar searchprogress = (progressbar) findviewbyid(r.id.searchprogress);      viewtreeobserver vto = searchbutton.getviewtreeobserver();     vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {         @override         public void ongloballayout() {             searchprogress.setlayoutparams(new linearlayout.layoutparams(searchbutton.getmeasuredwidth(), searchbutton.getmeasuredheight()));         }     }); } 

it's fast , dirty solution , may cause performance problems, when button size depends on progressbar size (it creates circular dependency , may cause infinite loop).

more practical , correct solution creating custom view, consists of progressbar , button. overriding onmeasure in view, can explicitly control size of parts.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -