Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Last active March 2, 2016 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/1b02dc228d4306ce073a to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/1b02dc228d4306ce073a to your computer and use it in GitHub Desktop.
code snippets for databinding post
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
viewModel = new LoginViewModel(this);
binding.setLogin(viewModel);
setSupportActionBar(binding.toolBar);
}
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="login" type="com.celites.aamantran.ViewModels.LoginViewModel"/>
</data>
<LinearLayout ...>
<LinearLayout ...>
<android.support.design.widget.TextInputLayout ...>
<EditText android:id="@+id/userNameEditText"
android:hint="@{login.userName}"
.../>
</android.support.design.widget.TextInputLayout>
<Button android:onClick="@{login.verify}"
android:id="@+id/btnSendVerification" .../>
</LinearLayout>
</LinearLayout>
</layout>
@Override
protected void onCreate(Bundle savedInstanceState) {
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)
}
});
}
<LinearLayout ...>
<LinearLayout ...>
<android.support.design.widget.TextInputLayout ...>
<EditText android:id="@+id/userNameEditText" .../>
</android.support.design.widget.TextInputLayout>
<Button android:id="@+id/btnSendVerification" .../>
</LinearLayout>
</LinearLayout>
//In your project's build.gradle verify this
classpath 'com.android.tools.build:gradle:1.5.0'
// In each module's build.gradle
android{
dataBinding {
enabled = true
}
}
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
User user = new User("Test", "User");
binding.setUser(user);
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"/>
</LinearLayout>
</layout>
public class User {
public final String firstName;
public final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
@BindingAdapter({"binding"})
public static void bindEditText(EditText view, final ObservableString bindableString) {
view.addTextChangedListener(new DefaultTextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
bindableString.set(s.toString());
}
});
String newValue = bindableString.get();
if (!view.getText().toString().equals(newValue)) {
view.setText(newValue);
}
}
@BindingAdapter({"binding"})
public static void bindSwitch(CompoundButton button, final BindableBoolean bindableBoolean) {
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bindableBoolean.set(isChecked);
}
});
boolean isChecked = bindableBoolean.get();
if (button.isChecked() != isChecked) {
button.setChecked(isChecked);
}
}
@BindingAdapter({"binding"})
public static void bindSwitch(SwitchCompat button, final BindableBoolean bindableBoolean) {
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bindableBoolean.set(isChecked);
}
});
boolean isChecked = bindableBoolean.get();
if (button.isChecked() != isChecked) {
button.setChecked(isChecked);
}
}
@BindingAdapter({"binding"})
public static void bindTextViews(TextView textView, final ObservableString bindableString) {
String newValue = bindableString.get();
if (!textView.getText().toString().equals(newValue)) {
textView.setText(newValue);
}
}
@BindingAdapter({"binding"})
public static void bindSwitch(CompoundButton button, final BindableBoolean bindableBoolean) {
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bindableBoolean.set(isChecked);
}
});
boolean isChecked = bindableBoolean.get();
if (button.isChecked() != isChecked) {
button.setChecked(isChecked);
}
}
@BindingAdapter(value = {"visibleIf", "inverse"}, requireAll = false)
public static void changeViewDependency(View view, BindableBoolean bool, boolean inverse) {
int visibility = bool.get() ? View.VISIBLE : View.GONE;
int visibilityInverse = bool.get() ? View.GONE : View.VISIBLE;
view.setVisibility((inverse) ? visibilityInverse : visibility);
}
<android.support.v7.widget.SwitchCompat
app:binding="@{eventViewModel.toAllDay}"
/>
<Button
app:binding="@{eventViewModel.toTime}"
app:visibleIf="@{eventViewModel.toAllDay}"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment