1

I have a class extends ViewGroup and want to get every MotionEvent from it. So far I have this:

class TestViewGroup extends ViewGroup {
     public TestViewGroup(Context context) {
         super(context);
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {
         Log.d("TestViewGroup", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
         return true;
     }
 }

The onTouchEvent(MotionEvent event) method is able to capture a MotionEvent every time I place my finger on the screen. But if I move my finger around the screen while my finger is still down, it won't continue to trace the coordinates of my finger. I know that in a class that extends a View, it is possible to keep tracing the finger as it moves through the View. I'm just wondering how it is possible to apply the same idea to a ViewGroup.

Brian
  • 7,955
  • 16
  • 66
  • 107

2 Answers2

0

I implemented a very simple ViewGroup class and copied your code, the onTouchEvent worked fine

The only thing I did differently was implement all of the constructors, though in general I would also call the super for the onTochEvent class that did not seem to make a difference.

So I am wondering if there is any code/xml you are leaving out that might make a difference?

Captures all touch events in emulator and on device, pretty much just like yours with the exception of the constructors.

public class customViewGroup extends ViewGroup {

    public customViewGroup(Context context) {
        super(context,null,0);
   }

    public customViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs,0);

    }

    public customViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
        return true;//super.onTouchEvent(event);
    }

}
Idistic
  • 6,281
  • 2
  • 28
  • 38
  • You know, you're right, its nothing to do with View or ViewGroup, it's actually got to do with how I'm integrating it I think. I'm trying to put the ViewGroup into the system's WindowManager so it can stay persistent on a system overlay (basically show a view no matter what activity is on top). Do you know if adding a View to the WindowManager would affect how the touch events are passed to it? – Brian Jul 17 '11 at 06:01
  • @AeroDroid ... sounds like an interesting thing to do, I would suggest opening another question and re-phrasing, also add some code. You might also try @Override public boolean dispatchTouchEvent(MotionEvent event) {} as an alternative since it seems to be a bit more robust in getting events, you will need to add some code though – Idistic Jul 17 '11 at 18:48
  • Actually I just opened a new question a while ago, here please take a look at it [here](http://stackoverflow.com/questions/6725636/getting-all-motionevents-with-windowmanager-layoutparams-flag-watch-outside-touch). – Brian Jul 17 '11 at 19:00
-1

you should add viewGroup.setClickable(true); to make sure the view can receive more touchevent.

serejja
  • 22,901
  • 6
  • 64
  • 72