Skip to content Skip to sidebar Skip to footer

Android How To Use Both Onclick And Long Press(3 Sec) For Same Button

In my application I need to do some Action A while doing normal click for the button and also need to do some other Action B while holding the same button for more than 3 sec. I ha

Solution 1:

Jamal. You really need to wait for 3 secs?

If you don't need to wait for 3 secs, use the methods setOnClickListener for single click actions, and setOnLongClickListener for long click actions.

You can check it out in the following links:

https://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

https://developer.android.com/reference/android/view/View.html#setOnLongClickListener(android.view.View.OnLongClickListener)

--

In the case you really need to wait for 3 secs, you have to check the difference between the timestamp of the ACTION_UP and ACTION_DOWN.


EDIT:

publicclassYourClass {    
privateMotionEventmLastEvent= MotionEvent.ACTION_UP;

finalRunnablerunr=newRunnable() {

                    @Overridepublicvoidrun() {
                        if(mLastEvent == MotionEvent.ACTION_MOVE) {
                            // Your code to run on long click
                        }
                    }
                };

finalHandlerhandel=newHandler();
contactButton.setOnTouchListener(newView.OnTouchListener() {

    @OverridepublicbooleanonTouch(View arg0, MotionEvent arg1) {
        mLastEvent = arg1.getAction();
        switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    handel.postDelayed(runr,3000);
                    break;          
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                default:
                    handel.removeCallbacks(runr);
                    break;

            }
                returntrue;
        }
    });

}

Solution 2:

All answers and comments are true. Here is your implementation

Button button;
long down,up;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    button.setOnTouchListener(newView.OnTouchListener() {
        @OverridepublicbooleanonTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN :
                    Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show();
                    down=System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP :
                    Toast.makeText(MainActivity.this, "Up", Toast.LENGTH_SHORT).show();
                    up=System.currentTimeMillis();
                    if(up-down>3000)
                        Toast.makeText(MainActivity.this, "More than 3", Toast.LENGTH_SHORT).show();
                    returntrue;
            }
            returnfalse;
        }
    });
}

}

Post a Comment for "Android How To Use Both Onclick And Long Press(3 Sec) For Same Button"