Can't Make a Stopwatch using onTouchListener

406
January 28, 2017, at 2:41 PM

So here i was using this current code but, not working.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);   
        mChronometer = new Chronometer(this);
        Timercount = (TextView)findViewById(R.id.timer_count);
        touch = (Button)findViewById(R.id.btn_touch);
        touch.setOnTouchListener(this);
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(v==touch){
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                 //TOUCH
                v.setOnTouchListener(mStartListener);
                return true;
            case MotionEvent.ACTION_UP:
                // RELEASED
                v.setOnTouchListener(mStopListener);
                return true;
              }
          }
          return false;
       }

and this is to format the time

View.OnTouchListener mStartListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    long elapsedMillis;
             int millis  = (int) (elapsedMillis % 1000);
             int seconds = (int) (elapsedMillis / 1000) % 60;
             int minutes = (int) ((elapsedMillis / (1000*60)) % 60);
             int hours   = (int) ((elapsedMillis / (1000*60*60)) % 24);
             StringBuilder sb = new StringBuilder();
             sb.append((char)('0' + hours / 10))
               .append((char)('0' + hours % 10)).append(":")                 
               .append((char)('0' + minutes / 10))
               .append((char)('0' + minutes % 10)).append(":")
               .append((char)('0' + seconds / 10))
               .append((char)('0' + seconds % 10)).append(".")
               .append((char)('0' + '0' + '0' + millis / 1));
             Timercount.setText(sb);             
         //I was using chronometer before but not working very well, it just count M:SS and random the time like "8:20"
             //mChronometer.setBase(hours + minutes + seconds + millis);
            //mChronometer.start();
       return false;
       }
   };   

I want to make it show like

00:00:00.000

so it will count from 1 millisecond to 999 millisecond, but it show like this in emulator

    View.OnTouchListener mStopListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
           //I purpose to saved the time when it stop, like using sharedpreferences or something like that
            return false;
        }
  };     

Anyone can help me to fix my code? My code was not working and i was confuse what to do next

Rent Charter Buses Company
READ ALSO
GridBagLayout component placing

GridBagLayout component placing

I'm having a problem with this layoutHow do I make the third button 2x2 in size with gridwidth?

390
jacob to call myOcx, messagemap not work

jacob to call myOcx, messagemap not work

I want to call my old ocx from java

402
How to get information along a specific route for google map android?

How to get information along a specific route for google map android?

With GoogleMapsetTrafficEnabled(true) I can get the traffic information for the whole map only

433