2
@Override
public boolean onTouchEvent(MotionEvent event) {

if(event.getAction()==MotionEvent.ACTION_DOWN){
Log.d(VIEW_LOG_TAG, "Touching Down");
}
    if(event.getAction()==MotionEvent.ACTION_UP){
Log.d(VIEW_LOG_TAG, "Not Touching");
}
}

I want Log-->"Touching Down" should be displayed in Log continuously until finger is released.

Please Help

Its Really Important...I cant Proceed further in ma Project without this.

Avtar Singh
  • 59
  • 1
  • 9
  • possible duplicate of [How to get a continuous Touch Event?](http://stackoverflow.com/questions/2539524/how-to-get-a-continuous-touch-event) – Bishan Jul 28 '14 at 15:04

2 Answers2

6

You cannot do that in the UI-Thread. Code running in UI-Thread must be short to keep UI responsive.

So you need to create a thread.

  • start the thread when ACTION_DOWN
  • In the thread : write a loop with your log (use a flag to stop the loop)
  • when ACTION_UP : change the flag (this will cause the end of the loop in your thread.

Sample code :

AtomicBoolean actionDownFlag = new AtomicBoolean(true);

Thread loggingThread = new Thread(new Runnable(){
     public void run(){
         while(actionDownFlag.get()){
             Log.d(VIEW_LOG_TAG, "Touching Down");
             //maybe sleep some times to not polute your logcat
         }
         Log.d(VIEW_LOG_TAG, "Not Touching");
     }
});

@Override
public boolean onTouchEvent(MotionEvent event) {

    if(event.getAction()==MotionEvent.ACTION_DOWN){
        loggingThread.start();
    }
    if(event.getAction()==MotionEvent.ACTION_UP){
        actionDownFlag.set(false);
    }
}
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Thanks...This is Great...I think it might Work in ma Program...let me check... Is there any other way without using thread...because I am already using a Thread in another class..."MainThread.java" – Avtar Singh Jul 28 '14 at 15:17
  • There is no problem to use different/multiple thread. As an alternative : you can look at the answer of @Heinrisch using handler and delayed messages. – ben75 Jul 28 '14 at 15:20
  • Ok...Thanks...Let me Work it Out...(Y) – Avtar Singh Jul 28 '14 at 15:33
2

You will only get one event for each touch update. So you will get ACTION_DOWN, then probably ACTION_MOVE, and ACTION_UP when the user releases.

You can either do something that is repeating until you vet ACTION_UP, or you also trigger on ACTION_MOVE depending on your requirements.

Something like this:

Handler handler = new Handler();
boolean pushingDown = false;
Runnable repeater = new Runnable() {
    @Override
    public void run() {
        Log.e("pressing down", "pressing down");
        if (pushingDown) {
            handler.postDelayed(this, 10);
        }
    }
};

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        pushingDown = true;
        handler.post(repeater);
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        pushingDown = false;
    }

    return super.onTouchEvent(event);
}
Heinrisch
  • 5,835
  • 4
  • 33
  • 43