Skip to content Skip to sidebar Skip to footer

How To Count Button Press Time In Android?

I am Developing an project in which i want diffrent task for diffrent time like if user button up after 5 milisecond then a code execute and if pressing time is more then that then

Solution 1:

I faced it since 3 month ago, here is my solution:

 private int count = 0;
    Button btn;
    private final Handler mHandler = new Handler();
    private Runnable mTimer1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        mTimer1 = new Runnable() {

            @Override
            public void run() {
                count++;
                // TODO Auto-generated method stub
                mHandler.postDelayed(this, 500);
                if (count == 1) {
                    mHandler.removeCallbacks(mTimer1);
                    // put your code here:
                Intent i;
                PackageManager manager = getPackageManager();
                try {
                   i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
                if (i == null)
                    throw new PackageManager.NameNotFoundException();
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i);
                } catch (PackageManager.NameNotFoundException e) {

                }

                }
            }
        };
        btn.setOnTouchListener(new OnTouchListener() {

            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, final MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = (int) btn.getX();
                    initialY = (int) btn.getY();
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    mHandler.postDelayed(mTimer1, 0);
                    return true;
                case MotionEvent.ACTION_UP:
                    count = 0;
                    return true;
                case MotionEvent.ACTION_MOVE:
                    runOnUiThread(new Runnable() {
                        public void run() {
                            btn.setX(initialX
                                    + (int) (event.getRawX() - initialTouchX));
                            btn.setY(initialY
                                    + (int) (event.getRawY() - initialTouchY));
                        }
                    });

                    return true;
                }
                return false;
            }
        });

with this code you can move your chathead and the event start after 500 milisecond. No need to use OnClickListener, check if position not change so detect it as OnClickListener.
And if you want to stop OnTouch event when catch some action, just put 'boolean variable' in ACTION_MOVE


Solution 2:

You can use View.OnTouchListener and two timestamp to store the button click start (ACTION_DOWN) and end time (ACTION_UP). Then, difference between this 2 variables (end-start) will tell you the duration of button click time and you can execute code according to your preference.

test.setOnTouchListener(new OnTouchListener() {
    private long start = 0;
    private long end = 0;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            this.start = System.currentTimeMillis();
        } else if (event.getaction()==MotionEvent.ACTION_UP) {
            this.end = System.currentTimeMillis();
        }
    }
});

Solution 3:

If you want do have custom durations you have to program it by yourself.

However it is recommended to use the android build in functions. There are a View.OnLongClickListener and a View.OnClickListener to distinguish from two different durations for clicking on views.


Solution 4:

i can't tell you the full exact code but i can explain the flow here, what i get from your answer is that you need to do different actions on the basis of last time the button is pressed, this is how you can do. You need two Date variables i.e. current and lastTime

  DateTimeUtils.getCurrentUTCFormattedDateTime()

store the lastTime in preferences on action down fetch the last time from preferences and get the current time and subtract it, to get the difference.

  long difference = (current.getTime() - lastSaved.getTime()) / 1000;

give you time difference in seconds, then you decide what to do. Hope it help :)


Post a Comment for "How To Count Button Press Time In Android?"