Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DataBinding library for binding events

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.

like image 741
Pankaj Avatar asked Aug 12 '15 09:08

Pankaj


People also ask

What is the difference between databinding and view binding?

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.

Can we use databinding in MVP?

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.

How do I use Kotlin data binding?

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 .


2 Answers

I think you will need to bind the handlers as well, maybe something like this in onCreate:

MyHandlers handlers = new MyHandlers(); binding.setHandlers(handlers); 
like image 164
justHooman Avatar answered Sep 28 '22 10:09

justHooman


Many Ways for setting Click

  1. Pass handler to binding.

    ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); Hander handler = new Handler(); binding.setHandler(handler);

  2. 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

like image 36
Khemraj Sharma Avatar answered Sep 28 '22 10:09

Khemraj Sharma