C0D3LIC1OU5
C0D3LIC1OU5

Reputation: 8680

Parameterless OnClick method call with DataBinding

I have a custom Presenter class and I cannot seem to get around having to pass an android.view.View object as a parameter into a method I want executed when user clicks on a button, even though I do not need the View object. Using DataBinding, is it possible to bind an onClick event to a method without parameters from XML?

<layout>
   <data>
     <variable
            name="presenter"
            type="com.somepackage.Presenter"/>
    </data>
    ...
    android:onClick="@{ presenter.onClick }"
    ...
</layout>

In the Presenter class, I must have the following signature, or code doesn't compile:

...
public void onClick( @NonNull View ignored ) {
     //do something that doesn't use the view parameter
}
...

What I would like to do is to have a method signature in my Presenter calss that doesn't have a View as a parameter:

...
public void onClick() {
     //do something that doesn't use the view parameter
}
...

Is that possible?

Upvotes: 1

Views: 1413

Answers (1)

cherif
cherif

Reputation: 1344

In your xml you have to do android:onClick="@{() -> presenter.onClick() }" to call your method

Upvotes: 3

Related Questions