1

I am building an app with a panic alert button. I want to display a toast message after I hold the button for 3 seconds so if the person hits it by mistake it wont send but I am not sure how to do it.

public long startTime = 0;

Button btn = (Button)findViewById(R.id.btnAlert);

       btn.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if((System.currentTimeMillis()- startTime)> 3000){
                        Toast.makeText(getApplicationContext(), "Alert Received! Emergency Services Will Arrive At Your Location Shortly", Toast.LENGTH_LONG).show();
                    }
                    return true;
                }
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    startTime = System.currentTimeMillis();
                    return true;
                }
                return false;
            }
        });
Cœur
  • 37,241
  • 25
  • 195
  • 267
Martin Hoban
  • 167
  • 13
  • I have tried putting a timer on the toast message so it will display it after 3 seconds but i would like to have the timer on the button instead – Martin Hoban Nov 01 '14 at 15:03
  • http://stackoverflow.com/questions/2750835/buttondown-and-buttonup-events-for-android-screen-buttons – codePG Nov 01 '14 at 15:06

1 Answers1

1

Do it by onTouchEvent. To each View you can add an onTouchListener(). When you receives an ACTION_DOWN event you will read the actual time and store it in startTime variable. Then if you receives an ACTION_UP event you will subtract actual time by startTime. If the time between startTime and actual time is greater than 3000 milliseconds you will show an Toast message. I can't test the following code right now but it should work.

public long startTime = 0;

button.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_UP){
            if((System.currentTimeMillis() - startTime) > 3000) {
                //show toast message
            }
            return true;
        }
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            startTime = System.currentTimeMillis();
            return true;
        }
        return false;
        }
});
Lubo
  • 384
  • 1
  • 5
  • when i test the app now the button wont respond to click/touch – Martin Hoban Nov 01 '14 at 20:23
  • get this error "ie/itsligo/medi_cation/Main$1#onTouch should call View#performClick when a click is detected" – Martin Hoban Nov 01 '14 at 20:52
  • here you go: http://stackoverflow.com/questions/24952312/ontouchlistener-warning-ontouch-should-call-viewperformclick-when-a-click-is-d – Lubo Nov 02 '14 at 08:09
  • i added the performClick event but the button is still disabled. I put the code just after the ACTION_UP event im not sure if thats correct place? – Martin Hoban Nov 02 '14 at 10:56
  • What do you mean under: button is still disabled? If you are using something else for button ie: classic View with background then you have to add this option to the XML android:clickable="true". http://stackoverflow.com/questions/11700975/set-up-android-view-clickable – Lubo Nov 03 '14 at 19:08
  • before i added in the above code i could press the button to display a toast but the button is not clickable anymore tried android:clickable="true" still no change – Martin Hoban Nov 06 '14 at 17:13
  • update your question with your actual code. It's very hard to debug a program without a source code :) – Lubo Nov 08 '14 at 18:44
  • did you removed the onClickListener? If no, then maybe that is the cause. Otherwise try to use Log.d at different places in your onTouchListener code and you will see what is happening. – Lubo Nov 09 '14 at 18:24