How to implement OnTouch listener in XML

As I know, we can define the onClick tag in xml after writing to xml, which we can easily use in java code by the name specified in xml, for example

<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 there a way to define onTouch in XML, if so, how to use ???

2) Or any other way to implement the onTouch listener inside the onClick listener;

here my goal is to interact with more than 100 buttons without specifying a button name, as follows: and also have onTouch functionality ...

  Button mbutton; mbutton = (Button)findViewbyId(R.id.button); 

thanks

+6
source share
3 answers

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).

+4
source

As far as I know, you cannot enter code directly in XML. Please correct me if I am wrong. You must have a code in the Activity class.

0
source
 Hope this helps some one You can easily do it using databinding: step1 public class DataBidingAdapter{ @SuppressLint("ClickableViewAccessibility") @BindingAdapter("touchme") public static void setViewOnTouch(TextView view, View.OnTouchListener listener) { view.setOnTouchListener(listener); } } step 2 in xml <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="handlers" type="com.user.handlers.Handlers" /> </data> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" > <EditText android:id="@+id/search" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/search_for_chats_title" app:touchme="@{handlers.searchPatient}" /> </FrameLayout> </linearLayout> </layout> step 3 public class Handler{ public boolean searchPatient(View v, MotionEvent event) { if (MotionEvent.ACTION_UP == event.getAction()) { //your code } return true; } } 
0
source

Source: https://habr.com/ru/post/1238469/


All Articles