Reputation: 834
How it is possible to set an android:onClick method AND an OnTouchListener on the same button? I really need this for my app.
I know about the differences of them and taking only android:onClick or OnTouchListener does work. As soon as I try to do both, just the OnTouchListener works.
I need this for different functionalities in different layouts.
Just for better understanding. With android:onClick I meant the method you assign this button to in the xml file like android:onClick="InitMethod"
Upvotes: 2
Views: 8211
Reputation: 425
Yes you can use both onClick and onTouch on a same button, but OnTouch callback you'll get motionEvent like ACTION_MOVE, ACTION_UP , ACTION_DOWN etc, Don't forget to return false (Details)in onTouch callback. Please refer the below code
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("test", "ontouch");
return false;
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("test", "onclick");
}
});
Just do the operations you want to do in the call backs onTouch and onClick respectively. Please NOte click is a action performed when user press the button and release but Touch will be taken when user presses it.
So on a single click the log will be like this. 1.ACTION_DOWN, 2.ACTION_UP 3. onClick
03-22 16:19:39.735: D/test(682): ontouch
03-22 16:19:39.735: D/test(682): ontouch
03-22 16:19:39.735: D/test(682): onclick
Upvotes: 12