Reputation: 74
I have a custom view which i am adding to relative layout and adding touch Listener to the custom view only.The problem is onTouchevent is not called.Plz help sample code from my actual code::
ViewGroup base = (ViewGroup)findViewById(R.id.base); //relative layout
base.addView(move);
move.setOnTouchListener(touchListener);
OnTouchListener touchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("onTouch v","="+v);
if(v instanceof MovingView) {
return false;
}
else
return true;
}
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
}
};// close listener
Upvotes: 1
Views: 1675
Reputation: 4081
First of all, you set the listener before initializing it (it is null). Try this:
ViewGroup base = (ViewGroup)findViewById(R.id.base); //relative layout
base.addView(move);
OnTouchListener touchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("onTouch v","="+v);
if(v instanceof MovingView) {
return false;
}
else
return true;
}
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
}
};// close listener
move.setOnTouchListener(touchListener);
Secondly, be aware that:
"onTouchEvent() method is called when a touch screen event was not handled by any of the views under it."
Upvotes: 1