2

I'm making a button that increments the value when press once and hold press the button. As long as the button is hold, the value will keep increasing. I've been told to use onTouch instead of onClick to do this, but couldn't find the right way to do so. The below code works for only press once.Thanks.

public class MainActivity extends AppCompatActivity {
int i = 0;
TextView textView;
Button button;


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button=findViewById(R.id.button);
    textView=findViewById(R.id.textView);

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

            if (event.getAction() == MotionEvent.ACTION_UP){
                setText();
            }
            return true;
        }
        });

}//oncreate
public void setText(){
    textView.setText(""+i);
    i++;
}
}//class
Grey
  • 147
  • 1
  • 2
  • 12

4 Answers4

2

combining MotionEvent.ACTION_DOWN and timer is the key. you will launch a timer on touch down d cancel on touch up, like this:

in activity:

//timer for increment
Timer fixedTimer = new Timer();

void initTimer() {
    fixedTimer = new Timer();

}

when register timer:

btn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                fixedTimer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        //increment here
                    }
                }, DELAY,INTERVAL_PERIOD_FOR_INCREMENT);
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                //cancel timer
                fixedTimer.cancel();
            }

            return false;
        }
    });
maheryhaja
  • 1,617
  • 11
  • 18
1

You need to use MotionEvent.ACTION_DOWN use below code to get the proper result.

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

        if (event.getAction() == MotionEvent.ACTION_DOWN){
            setText();
        }
        return true;
    }
    });

Now when you hold the button down it will increase the value and on action up it will do nothing.

Hope this will help.

Sukhbir
  • 1,410
  • 14
  • 33
0

Replace MotionEvent.ACTION_UP to

MotionEvent.ACTION_DOWN

for button pressed

mButton.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                    Log.d("Pressed", "Button pressed");
                else if (event.getAction() == MotionEvent.ACTION_UP)

                 Log.d("Released", "Button released");
                // TODO Auto-generated method stub
                return false;
            }
        });
Himanshu itmca
  • 395
  • 5
  • 22
0

try this code:

final CountDownTimer countDownTimer=new CountDownTimer(Long.MAX_VALUE,1000) {
            @Override
            public void onTick(long l) {
                int i= Integer.parseInt(finalTxtView.getText().toString());
                finalTxtView.setText(String.valueOf(++i));
            }

            @Override
            public void onFinish() {

            }
        };

        btnPresMe.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()==MotionEvent.ACTION_DOWN)
                {
                    countDownTimer.start();
                }
                if (motionEvent.getAction()==MotionEvent.ACTION_UP)
                {
                    countDownTimer.cancel();
                }
                return false;
            }
        });

It will increase value after every 1 second passed when you hold the button. you can change 1000 (1 sec) as per your requirement.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46