to Controller of MVC, and make it modular • Does not (entirely) replace role of activities and fragments as controller. • ViewModel defines rule of handling views based on Model. • Leaving controlling part - (Db, Network screen routing) on activities and fragments.
plugin to v1. 5 classpath 'com.android.tools.build:gradle:1.5.0' • In your module’s build.gradle file update your android{} dsl with databinding.enabled=true android { // Other Logic dataBinding { enabled = true } // Other Logic }
super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); EditText userName = (EditText) findViewById(R.id.userNameEditText); Button sendVerification = (Button) findViewById(R.id. btnSendVerification); sendVerification.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //TODO: Check username is null or not //TODO: Send it to webservice, (if network is there, otherwise save somewhere to send it later) } }); }
android:text="@{user.firstName}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.lastName}"/> • In Java - User Class public class User { public final String firstName; public final String lastName; public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } }
+ Binding ◦ For layout activity_place_details, binding class name is ActivityPlaceDetailsBinding • For each variable, you need to set data. • For each property, there must be a public getter equivalent to it ◦ E.g. for @{user.firstName} there must be public variable or public getter for it
Xml. • For Read only presentation. • Can not read-validate form • Can not update status based on child/sibling view’s property *- By default, However it’s possible *
Get to read value. Set to right value. • Nearly thread safe • Static methods - Errors on duplicate implementation • Single method can listen to multiple attributes. @BindingAdapter Annotation • Can convert from one object to another @BindingConversion Annotation
• Define view rules in view model and expose them as getters • Use view models in xml and refer to rules • Set BindingAdapters, BindingConversions to establish a rule • And again, one binding adapter should be common for whole project
from Lisa Wray - https://plus.google. com/+LisaWrayZeitouni/posts/LTr5tX5M9mb • Visibility using Boolean - Tip From Lisa Wray - https://plus.google. com/+LisaWrayZeitouni/posts/5DQGiNbne5W • Tinting - Tip from Luis G Vale - https://plus.google.com/+LuisGonz%C3% A1lezValle/posts/YjUcVJ14Rf5 • Datepicker databinding - George Mount ( A googler) https://halfthought.wordpress. com/2015/10/16/datepicker-data-binding/ • RTL Support - Yours truly - https://plus.google.com/+PrashamTrivedi/posts/gCxFGLV498h