Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Databinding onLongClick not working

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" />
like image 949
M P Mathugama Avatar asked Oct 11 '17 06:10

M P Mathugama


Video Answer


2 Answers

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;
}
like image 63
younes Avatar answered Sep 23 '22 14:09

younes


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.

like image 34
M P Mathugama Avatar answered Sep 21 '22 14:09

M P Mathugama