1) Is there a way to define onTouch in XML, if so, how to use ???
No , the default attribute provided by android does not allow to determine the touch event in the xml file, you need to write this method in the .java file.
2) Or any other way to implement onTouch Listener inside the onClick listener;
No , you cannot implement onTouch() inside onClick() because the onClick event is fired when you touch or speak, tap or tap the screen for the first time and execute the event when you release the touch screen. But he is not able to detect the movement of your touch, i.e. ACTION_DOWN , ACTION_UP , etc.
So, if you want to implement the onTouch() event, you will write it yourself.
You can write some kind of mechanism that will allow you to easily implement the onTouch() event. But instead, I suggest you use the Butterknife library, which allows you to easily write the onTouch() method by simply defining annotation . Here's the code , how they are attached to onTouch () in the annotation (in case you want to write your own mechanism).
source share