4

I am not able to get the second view id where the finger is moved. I tried to print the id where the pointer is using the following in the on touch listener.

    case MotionEvent.ACTION_MOVE:
            int pointerCount = event.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                //System.out.println(v.getId());
                System.out.println(event.getX() + "---" + event.getY());
                System.out.println(v.getId());
            }
            break;

Here event is my MotionEvent and v is the View. I am able to get the X and Y positions of the touch, but i want to know which view is being crossed. I get only the view id of the first view that was pressed all the time.

I have checked the following links for some help, but was unable to get any relevant details.

How do I get the ID of the moving finger

retrieving the id of the view that was clicked

how to get the particular sub view id from the view in android?

Can somebody please help me with this?

Community
  • 1
  • 1
Mohit Ajwani
  • 1,328
  • 12
  • 24

1 Answers1

0

If you set a listener on a view you'll always get that view inside the event. For example

View one = findById(R.id.one);
one.setOnTouchListener(new OnTouchListener()
{
  public void onClick(View v)
  {
      // v == one
  }
})

But if you declare your Activity as the touch listener:

Activity implements OnTouchListener

Then you set the listener on the root view:

View rootView = findViewById(R.id.rootView);
rootView.setOnTouchListener(this);

And override onTouch:

@Override
public boolean onTouch(View v, MotionEvent e) { 
    // here v is what you look for
} 

I think you should check if you're setting onTouchListener on root view and not one particular view

Javi Mollá
  • 786
  • 7
  • 18
  • Hey @Javier, I tried that, but didn't help. I have a relative layout and inside which i have a few image buttons which overlap each other. When i press a button, i can detect the touch. But the problem is with dragging the finger to the second button, as soon as i touch the other button to which i move, i cannot detect the second view, the id that i get is still of the first view. – Mohit Ajwani Feb 18 '15 at 11:54
  • Could you post how you are setting the touch listener? – Javi Mollá Feb 18 '15 at 15:48
  • I have implemented the listener as follows : `public class MyClass implements OnTouchListener, OnLongClickListener {} @Override public boolean onTouch(View v, MotionEvent event) { //Here I have used switch case to identify the id of the button that was pressed } ` – Mohit Ajwani Feb 19 '15 at 10:50
  • Ok, but how are you setting the touch listener? something like `View rootView = findViewById(R.id.rootView); rootView.setOnTouchListener(this);` – Javi Mollá Feb 19 '15 at 11:10