Last active
March 2, 2016 07:34
-
-
Save PrashamTrivedi/1b02dc228d4306ce073a to your computer and use it in GitHub Desktop.
code snippets for databinding post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout ...> | |
<LinearLayout ...> | |
<android.support.design.widget.TextInputLayout ...> | |
<EditText android:id="@+id/userNameEditText" .../> | |
</android.support.design.widget.TextInputLayout> | |
<Button android:id="@+id/btnSendVerification" .../> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity); | |
User user = new User("Test", "User"); | |
binding.setUser(user); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
public final String firstName; | |
public final String lastName; | |
public User(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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