14

I have a text view to which I need to create a listener for onLongClick. Right now for the respective viewmodel it has a function sendLogs() which deals with the logic for onClick. If I change onClick to onLongClick function never get call. Is there any way to make it work for onLongClick?

onClick is directly linked to my model class function but not the onLongClick. So I think model class binding is correct but I may need some extra work here.

<data>
    <import type="android.view.View" />

    <variable
        type="com.aaa.bbb.viewmodel.SystemSettingsViewModel"
        name="systemSettings"
    </variable>
</data>

<TextView
    android:gravity="end"
    android:id="@+id/tv_logging"
    android:layout_centerVertical="true"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:layout_width="wrap_content"
    android:onClick="@{() -> systemSettings.sendLogs()}"
    android:text="@string/enable_logs"
    android:textAlignment="viewEnd" />
Daivid
  • 627
  • 3
  • 12
  • 22
M P Mathugama
  • 1,418
  • 1
  • 20
  • 31

6 Answers6

52

I managed to work it correctly. I doubt this is properly documented.

In xml

android:onLongClick="@{(view) -> presenter.onLongClickOnHeading(view)}"

In presenter viewmodel class

public boolean onLongClickOnHeading(View v) { 
   //logic goes here
   return false; 
}

Note: this method signature should be exactly in this format. Otherwise biding errors will be thrown at runtime.

M P Mathugama
  • 1,418
  • 1
  • 20
  • 31
5

Here is the complete the code.

There is no such attribute for long click. So we have to create a binding adapter.

BindingUtils.kt

object BindingUtils {

    private const val ON_LONG_CLICK = "android:onLongClick"

    @JvmStatic
    @BindingAdapter(ON_LONG_CLICK)
    fun setOnLongClickListener(
        view: View,
        func : () -> Unit
    ) {
        view.setOnLongClickListener {
            func()
            return@setOnLongClickListener true
        }
    }
}

Layout

<androidx.constraintlayout.widget.ConstraintLayout
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:onLongClick="@{() -> vm.onLongClick()}"/> 
Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49
  • At one .xml file i used the 'onLongClick' without this method and it worked with no problem but at the other it did not, showing strange compile error. It was resolved when using this method. Thx. – epic Sep 12 '20 at 13:08
2

For it to work, the part in parenthesis has to match the method signature from the interface View.OnLongClickListener which looks like this :

boolean onLongClick(View view);

So this is how I got it to work :

<View
...
android:onLongClick="@{(view) -> listener.onLongClick(view, viewmodel)}"/>
...
Pafoid
  • 435
  • 4
  • 8
1

In the xml section, you must refer to the Boolean return function, such as the following code, so as not to get into trouble.in build project android studio

in xml

android:onLongClick="@{(view) -> presenter.onLongClick(view)}"

in java

  public boolean onLongClick(View v) {
    return false;
}
younes
  • 742
  • 7
  • 8
0

You should look into this document

OnLongClick is as easy as onClick

Within your SystemSettingsViewModel you can have

public boolean onLongClick(){}

and in xml

android:onLongClick="@{() -> presenter.onLongClick()}"
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 2
    Yes it should be. I read that document & tried out earlier. But no result. Compiler failed to compile with android:onLongClick() – M P Mathugama Oct 13 '17 at 07:28
  • 2
    Dealtwith the same issue, also the debugger does not show the exact problem but 150 lines of emptiness untill it tells me "1 error" as summary My solution was that the method in the listener/handler MUST be a boolean – MwBakker Dec 19 '18 at 08:46
0

As mentioned in the Google documentation Link there is no problem with what you wrote.

This is a sample of OnLongClick in XML:

android:onLongClick="@{(theView) -> presenter.onLongClick(theView, task)}"

class Presenter {
    fun onLongClick(view: View, task: Task): Boolean { }
}
Ehsan
  • 2,676
  • 6
  • 29
  • 56