0

I haven't seen an answer for what I am trying to do. I want to update code for as long as I am pressing down on a button and when I let go, the code stops updating. Any advice helps please. Here's my code:

forward.setOnTouchListener(new View.OnTouchListener() {


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // Code updates every frame
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP){
                // Code stops updating
                return true;
            }
            return true;
        }


    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mike-gallego
  • 706
  • 2
  • 12
  • 27
  • what do you want to update – vikas kumar Nov 08 '17 at 17:40
  • Please specify what exactly do you want to update. – Armin Suljovikj Nov 08 '17 at 19:06
  • What I want to update is irrelevant though. It could be anything I just want to make code execute as long as I am pressing down. For example If I want hello world to be printed out, it would do it for as long as I am pressing down. If I let go, hello world is not printed no more. @ArminSuljovikj – mike-gallego Nov 08 '17 at 19:10
  • I tested your code on my end and it works fine. I made a simple test by setting a TextView with different texts while I am holding and letting go off the button. I will post the example as an answer and tell me what is not working. – Armin Suljovikj Nov 08 '17 at 19:13
  • text1 doesnt update.@ArminSuljovikj – mike-gallego Nov 08 '17 at 19:22

1 Answers1

0

Here is the example:

 button1.setOnTouchListener(new View.OnTouchListener() {


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            text1.setText("Text1");
        } else if (event.getAction() == MotionEvent.ACTION_UP) {

            text1.setText("Text2");
        }
        return true;
    }


});