in my application,i want to get the position on touchevent.but when i click on button after that i want to call ontouchevent and onDraw() method and get the position. how can i do this?Please help me
public void getPos(View v){
//here i want to call onTouchEvent method..
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP) {
Log.d("position", event.getX() + "-" + event.getY());
return true;
}
return false;
}
You do not call those methods by yourself. All methods named on...
are callback methods that are called by the Android system when certain events happen. In your case this method (defined in the Activity) is called when there's a touch-event that's not handled elsewhere.
Touching the button is handled by the button, therefore the onTouchEvent of the Activity is not called.
What you could do, is to add an onTouchEventListener to your button:
So in your onCreate()
method (another method called by Android at the event of the acitity beeing created), find your button and add the listener like so:
View button = findViewById(R.id.your_button_id);
button.setOnTouchListener(new OnTouchListener() {
onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP) {
Log.d("position", event.getX() + "-" + event.getY());
}
return false;
}
}
Note, that I always return false
as to not to interfere with the button. If you return true
, the event will be "eaten up" and not passed along to be handled by views anymore. So you may tweak this at will.
Btw. as you can see, you can attach an OnTouchListener to any view, so perhaps you don't need an actual button at all.