0

I have image for adding 1 to some edittext, and I have written code to handle onclick event of that image and its running fine. But I have to implement that if the user presses the image it should increment the edittext and if he keeps the image in pressed state it should increment the edittext for more values which depends on how long he has kept the image pressed.

1 Answers1

0

What you could do is use a handler that executes a runnable in a continuous loop while the image is being pressed. The runnable would contain the increment code:

private int _startIncrementRate = 750;
private int _incrementRate = 50;
private Handler _handler;
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    _handler = new Handler();
    _img.setOnTouchListener(new View.OnTouchListener()
    {
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            switch (motionEvent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    IncrementEditText();
                    _handler.postDelayed(handler_run, _startIncrementRate);
                    break;
                case MotionEvent.ACTION_UP:
                    _handler.removeCallbacks(handler_run); // stop runnable from executing
                    break;
            }
            return false;
        }
    });
}
private Runnable handler_run = new Runnable()
{
    public void run()
    {
        IncrementEditText();
        _handler.postDelayed(this, _incrementRate);
    }
};
private void IncrementEditText()
{
    /* increment editText code here */
}
Richard
  • 560
  • 1
  • 8
  • 17