Android Progress Bar style programmatically -
i'm trying instantiate progress bar
main activity , add view dynamically. when this, bar:
progressbar bar = new progressbar(this, null, android.r.attr.progressbarstylehorizontal); layoutparams params = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); bar.setlayoutparams(params); bar.setid(mint); linearlayout layout = (linearlayout) findviewbyid(r.id.layout); layout.addview(bar);
i started new project includes above code, , progress bar displays correctly.
if add following lines above code, screen blank second , shows half full bar. expect see empty bar second , goes 50%.
try { thread.sleep(2000); } catch ...{ } bar.setprogress(50);
if add code this, however, bar , changes display correctly.
button b = new button(this); b.settext("change bar"); b.setonclicklistener(new onclicklistener() { public void onclick(view v) { changeprog(); } }); public void changeprog() { switch (state) { case 0: bar.setprogress(100); state = 1; break; case 1: bar.setprogress(127); state = 2; break; case 2: bar.setprogress(33); state = 0; break; } }
if try automate process loop call changeprog()
every often, screen stays blank - no widgets @ display, whether described xml or pro grammatically makes no difference.
i'm having trouble understanding why behavior exists.
it seems call thread.sleep(2000)
ui thread, causes problem, create anr. try create new thread (separate thread) change progress value.
see example below:
Comments
Post a Comment