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 implement
It is not obligatory to create the separate class MyHandlers and call setHandlers for processing android:onClick. You can just use the methods: public void onClickFriend(View view) and public void onClickEnemy(View view) in MainActivity.
The activity view:
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);
}
public void onClickFriend(View view) {
Log.i(MyHandlers.class.getSimpleName(), "Now Friend");
}
public void onClickEnemy(View view) {
Log.i(MyHandlers.class.getSimpleName(), "Now Enemy");
}
}
A layout:
Take a look at the example of using the Data Binding Library for the MVVM pattern: http://cases.azoft.com/mvvm-android-data-binding