Using DataBinding library for binding events
Solution 1:
I think you will need to bind the handlers
as well, maybe something like this in onCreate
:
MyHandlers handlers = new MyHandlers();
binding.setHandlers(handlers);
Solution 2:
Many Ways for setting Click
-
Pass handler to binding.
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); Hander handler = new Handler(); binding.setHandler(handler);
-
Set clicks (use any of below)
android:onClick="@{handler::onClickMethodReference}"
OR
android:onClick="@{handler.onClickMethodReference}"
OR
android:onClick="@{() -> handler.onClickLamda()}"
OR
android:onClick="@{(v) -> handler.onClickLamdaWithView(v)}"
OR
android:onClick="@{() -> handler.onClickLamdaWithView(model)}"
See Handler class for understanding.
public class Handler {
public void onClickMethodReference(View view) {
//
}
public void onClickLamda() {
//
}
public void onClickLamdaWithView(View view) {
//
}
public void onClickLamdaWithObject(Model model) {
//
}
}
Note that
- You can use Method Reference (::) when you have same argument as the attribute onClick.
- You can pass any object like
onClickLamdaWithObject
example. - If you need to pass
View
object then just use(v)->
expression.
Further reading
https://developer.android.com/topic/libraries/data-binding/expressions
Solution 3:
Use this format in your xml:
android:onClick="@{handlers::onClickFriend}"
Pay attention to the ::
, do not worry about the red lines in xml editor, because is currently this is open bug for the Android Studio xml editor.
Where handlers
is your variable from data tag:
<data>
<variable name="handlers" type="com.example.databinding.MyHandlers"/>
</data>
and onClickFriend
is your method:
public class MyHandlers {
public void onClickFriend(View view) {
Log.i(MyHandlers.class.getSimpleName(),"Now Friend");
}
}
ADDED
For handle onLongClick
in xml add this:
android:onLongClick="@{handlers::onLongClickFriend}"
and add onLongClickFriend
method in your ViewModel class:
public class MyHandlers {
public boolean onLongClickFriend(View view) {
Log.i(MyHandlers.class.getSimpleName(),"Long clicked Friend");
return true;
}
}
ADDED
If you need to show toast message, you can use interface (better variant), or pass context
in the MyHandlers
class in construction:
public class MyHandlers {
public boolean onLongClickFriend(View view) {
Toast.makeText(view.getContext(), "On Long Click Listener", Toast.LENGTH_SHORT).show();
return true;
}
}
Solution 4:
If you're going to use your activity, might as well replace the context
object that is automatically binded, otherwise you're wasting the space.
A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.
binding.setContext(this);
and
<variable name="context" type="com.example.MyActivity"/>
Note if you just use plain string onClick="someFunc"
that's not a databinding functionality at all. That's an older feature that uses a little reflection to find the method on the context.