java - Text Appears after progress bar - Android -
i playing around progress bar in android , want text appear after progress bar has run. text appears @ same time. in wrong place? should be?
package com.example.bmi; import android.os.bundle; import android.os.handler; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.edittext; import android.widget.progressbar; import android.widget.textview; public class mainactivity extends activity { private progressbar progressbar; private int progressstatus = 0; private handler handler = new handler (); //private textview textview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void calculateclickhandler(view view) { if (view.getid() == r.id.button1) { progressbar = (progressbar) findviewbyid(r.id.progressbar1); //textview = (textview) findviewbyid(r.id.textview1); // start long running operation in background thread new thread(new runnable() { public void run() { while (progressstatus < 100) { progressstatus += 1; // update progress bar , display //current value in text view handler.post(new runnable() { public void run() { progressbar.setprogress(progressstatus); //textview.settext(progressstatus+"/"+progressbar.getmax()); } }); try { // sleep 200 milliseconds. //just display progress thread.sleep(50); } catch (interruptedexception e) { e.printstacktrace(); } } } }).start(); edittext weighttext = (edittext) findviewbyid(r.id.edittext1); edittext heighttext = (edittext) findviewbyid(r.id.edittext2); textview result = (textview) findviewbyid(r.id.textview4); float weight = float.parsefloat(weighttext.gettext().tostring()); float height = float.parsefloat(heighttext.gettext().tostring()); float bmi = calcbmi(weight, height); float roundedbmi = (float) (math.round(bmi*100.0)/100.0); string yofat = fat(roundedbmi); result.settext("your bmi is: " + roundedbmi + "\n" + yofat); } } private float calcbmi(float weight, float height) { // todo auto-generated method stub return (float) (weight / (height * height)); } private string fat (float bmi) { if (bmi < 16) { return "yo skinny ass needs cake!"; } else if (bmi < 18.5) { return "stop eating them salads!"; } else if (bmi < 25) { return "grrrr, way it!"; } else if (bmi < 30) { return "put down cupcake!"; } else { return "ohh fat, love cake!"; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
the code directly under instantiated thread doesn't wait thread finish runnable before it's executed.
instead, re-enter ui thread @ end of new thread's runnable , set text:
new thread(new runnable() { @override public void run() { while (progressstatus < 100) { // progress update... } // progress finished, re-enter ui thread , set text handler.post(new runnable() { @override public void run() { edittext weighttext = (edittext) findviewbyid(r.id.edittext1); edittext heighttext = (edittext) findviewbyid(r.id.edittext2); textview result = (textview) findviewbyid(r.id.textview4); float weight = float.parsefloat(weighttext.gettext().tostring()); float height = float.parsefloat(heighttext.gettext().tostring()); float bmi = calcbmi(weight, height); float roundedbmi = (float) (math.round(bmi*100.0)/100.0); string yofat = fat(roundedbmi); result.settext("your bmi is: " + roundedbmi + "\n" + yofat); } }); } }).start();
Comments
Post a Comment