0

After a long time finally i got to know that how to update progress bar while button is being pressed. This code is not work for gingerbread. and all the other OS it works fine. in-case of any question you may ask enjoy!!!!

         button.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    if(arg1.getAction()==MotionEvent.ACTION_DOWN){

                        arg0.post(rotationRunnable);

                    }
                    return false;
                }
    });

RotationRunnable Method :-

 private Runnable rotationRunnable = new Runnable() {
        @Override
        public void run() {

            if (button.isPressed()) {
                try{
                if(mProgressStatus<100){
                    Thread.sleep(500);

                    mProgressStatus = doWork();
                    pB.setProgress(mProgressStatus);
                }
                }catch(Exception e) {
                    e.printStackTrace();
                }
                button.postDelayed(rotationRunnable, 40);
            }
        }
    };

and finaly DoWork Method :-

                      private int doWork() {
        // TODO Auto-generated method stub
        Timer t=new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                mProgressStatus=mProgressStatus+1;
            }
        }, 1000);
        time=mProgressStatus;
        return mProgressStatus;
    }
Hafiz.M.Usman
  • 223
  • 3
  • 21

1 Answers1

0

The problem might be the Thread.sleep(500) you are using. It might be blocking the main thread. Overall your code is a bit confusing to me. You use 3 different things to time your progress (thread.sleep of 500, 40ms of delayed posting and also the timertask)? couldn't you just do

private Runnable rotationRunnable = new Runnable() {
    @Override
    public void run() {

        if (button.isPressed()) {
            mProgressStatus++;
            pB.setProgress(mProgressStatus);
            button.postDelayed(rotationRunnable, 1000);
        }
    }
};
Ivo
  • 18,659
  • 2
  • 23
  • 35