android - Update ProgressBar onTouch and restart it on release (with imageButton) -


i trying achieve following design/functionality: have circular image surrounded circular progress bar. want when user touches imagebutton progress bar start progress that:

enter image description here

the proglem when user decides remove finger imagebutton (motionevent.action_up) progress bar not stop , return initial state (0). please share if have suggestions/improvements problem. thank in advance!

this code:

public class mainactivity extends appcompatactivity { public circleprogressbar progressbar; private static final string tag = mainactivity.class.getsimplename(); private int totalprogresstime = 100; thread t;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);     progressbar = (circleprogressbar)findviewbyid(r.id.custom_progressbar);     t = new thread() {         @override         public void run() {             int jumptime = 0;              while(jumptime < totalprogresstime) {                 try {                     sleep(200);                     jumptime += 5;                     final int finaljumptime = jumptime;                     runonuithread(new runnable() {                         @override                         public void run() {                             progressbar.setprogress(finaljumptime);                         }                     });                 }                 catch (interruptedexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             }         }     };      imagebutton imagebutton = (imagebutton)findviewbyid(r.id.imagebutton);     imagebutton.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view v, motionevent event) {             if(event.getaction() == motionevent.action_down) {                 if(!t.isalive()){                     t.start();                 }else {                     t.stop();                 }                 toast.maketext(mainactivity.this, "ontouch", toast.length_long).show();             }              if(event.getaction() == motionevent.action_up){                 t.interrupt();             }              log.d(tag, "event type: " + event.tostring());             return false;         }     });  } 

i think you're misunderstanding how use thread. bottom line shouldn't using thread in android. it's never advisable start thread activity (easy mistake if came java background , learning android; did same thing). also, read javadoc thread.stop. should never calling method, ever.

what want instead learn how use handler schedule tiny bits of works run on main thread @ interval. keep scheduling new bits perform rendering want until sets flag tell not schedule bit more. you'll solve problem without using separate thread @ all.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -