0

I want to do some action when button is pressed and stop those action when button is releasd. I have used the following code but it does not work. you may check my code here-

 ok = (Button)findViewById(R.id.button1);
 ok.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            my_var=true;
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                while(my_var) do_something();
            }else if (event.getAction() == MotionEvent.ACTION_UP) {
                my_var=false;
            }

            return true;
        }
    });

Is there any alternative solution for the above. Thanx in advance.

akshay
  • 5,811
  • 5
  • 39
  • 58
  • 1
    "Does not work" never help , please elaborate – meda Apr 12 '14 at 20:04
  • when you click on the button what happens?? – Prashant Patel Apr 12 '14 at 20:08
  • Unless `my_var` is initially `true` then `do_something()` will never be called. Even if it is `true`, when you release the touch (`ACTION_UP`) it is set to `false` and so the next touch won't work. – Squonk Apr 12 '14 at 20:13
  • @meda & @ prashant patel: it does do_something() until finish of while method. but I want to stop it when user releases button. – akshay Apr 12 '14 at 20:14
  • @prashant patel: do you really think, you'll require logcat ?? – akshay Apr 12 '14 at 20:18
  • @yahska Yes debug messages can helpful while tracking method's behavior. Try tu put `Log.d` or some thing to trace your method's behavoir – Prashant Patel Apr 12 '14 at 20:19
  • 2
    Have a look at this thread (especially the `AutoRepeatButton` answer) which is usually how I like to implement something like this: http://stackoverflow.com/questions/4284224/android-hold-button-to-repeat-action – Ken Wolf Apr 12 '14 at 20:29
  • @ken Wolf: that is exactly what I was looking for "AutoRepeatButton". thank you. – akshay Apr 12 '14 at 20:37

2 Answers2

1

I think what you want is an AutoRepeatButton

Basically it's a class that extends Button and adds some scheduled callbacks (you can adjust the interval) that happen as long as the ACTION_DOWN MotionEvent is occurring.

There are a few implementations floating around but I like the following one the best:

https://stackoverflow.com/a/8463940/833647

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • solution given above is working, but may be having some threading issues. So, can u suggest any other link. or any other example. – akshay Apr 17 '14 at 06:30
  • No sorry, that always has worked for me. What kind of threading issues? – Ken Wolf Apr 17 '14 at 06:48
  • I m moving object there, but sometimes button click doesn't work there. it doesn't move. – akshay Apr 17 '14 at 06:50
  • I can't really comment I'm afraid unless I see your solution. I feel I have answered your question - maybe someone else can help or you can open up a new question specific to this new problem. – Ken Wolf Apr 17 '14 at 06:51
  • 1
    yeah. bdw thanks for your answer. I'll mark it as accept as it works on dat prob. tank you. – akshay Apr 17 '14 at 08:10
0

To achieve so, you have to set my_var to false somewhere in the do_something method.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • but I want to set my_var to false when button press is released, so how can I do that in do_something()? – akshay Apr 12 '14 at 20:21
  • I guess you could pass the event as a parameter to the function and check there. – NiVeR Apr 12 '14 at 20:22