I'm trying to bind events with views in xml using DataBinding Library shipped with Android M. I'm following examples from Android Developers and implementing step-by-step. for the view's attributes like visibility,text its working fine but if I try to bind with onclick, it doesn't work as expected. Here's the sample code that I've tried so far:
<data> <import type="android.view.View"/> <variable name="user" type="com.example.databinding.User"/> <variable name="handlers" type="com.example.databinding.MyHandlers"/> </data> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName}" android:visibility="@{user.isFriend ? View.VISIBLE : View.GONE}" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:id="@+id/button" android:layout_gravity="left" android:onClick="@{handlers.onClickFriend}"/>
MainActivity :
public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); user = new User("Pankaj","Kumar",true,true); binding.setUser(user); } }
MyHandlers:
public class MyHandlers { public void onClickFriend(View view){ Log.i(MyHandlers.class.getSimpleName(),"Now Friend"); } public void onClickEnemy(View view){ Log.i(MyHandlers.class.getSimpleName(),"Now Enemy"); } }
I've written only required code to improve readability. Could someone help me on this.
View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.
Why use Data binding with Mvp? Combining Databinding along wih MVP pattern can result in a very clean structure and maintainable project. Databinding saves u a lot of stress and uneccesary long lines of code. Your UI is updated eaily and gone are those days where you need "findViewById" and onclick listeners and so on.
Launch Android studio and create a new project. Once the project is ready, go to the Gradle scripts folder and open build. gradle (module: app) . Add buildFeatures and set databinding to true .
I think you will need to bind the handlers
as well, maybe something like this in onCreate
:
MyHandlers handlers = new MyHandlers(); binding.setHandlers(handlers);
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
onClickLamdaWithObject
example.View
object then just use (v)->
expression.Further reading
https://developer.android.com/topic/libraries/data-binding/expressions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With