As i know we can define onClick tag in xml after writing in xml we can use in java code easily by name that specify in xml eg
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="myClick" />
public void myClick(View v) {
// does something very
}
1) Is any way to define onTouch
in XML if yes then how to use ???
2) Or any other way to implement onTouch
Listener inside onClick
listener;
here my goal is to interact more than 100 buttons without defining Buttons name as mention below: and also have onTouch
functionality...
Button mbutton;
mbutton = (Button)findViewbyId(R.id.button);
Thanks
1) Is any way to define onTouch in XML if yes then how to use ???
No, there is no default attribute provided by android that lets you define touch event in xml
file, you have to write the method in .java file.
2) Or any other way to implement onTouch Listener inside onClick listener;
No, you cannot implement onTouch()
inside onClick()
because onClick event gets called when you touch or say click or tap the screen for the first time and performs the event when you release touch. But it is not able to detect the movement of your touch i.e. ACTION_DOWN
, ACTION_UP
etc.
So if you want to implement onTouch()
event, you will have write it on your own.
You can write some mechanism that can let you implement onTouch()
event easily. But instead of doing that I'd suggest you to use Butterknife library which will let you write onTouch()
method easily just by defining annotation
. Here's the code how they have binded onTouch() in annotation (in case you want to write your own mechanism).