Android databinding is an awesome tool that enabels a clean MVVM architecture. However lacking from the core SDK is support for bound lists of buttons, radio buttons and checkboxes. These are pretty normal use cases that I encounter in almost every app I develop. Therefore I have published this library to aid in that.
Add a jitpack reference to your project.
- In your root
build.gradle
Add reference to the jitpack.io repoistory if you haven't got one already
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- In your application module
implementation 'com.github.nilzor:bindable-radiobuttons:'1.2.2'
- Expose a
Listof objects of type or subtype ofRadioButtonViewModelin your view model class. - Add a
<com.nilsenlabs.bindableradiobuttons.radiobuttons.BindableRadioButtonList>view to your XML and wire up:- A reference to your button list through the
app:buttonsproperty - (Optionally) Reference a custom view through the
app:itemViewIdproperty. The layout XML file must contain a single<RadioButton>view element - (Optionally) A one- or two-way binding to a viewmodel-property using the
app:selectedItemproperty. The field must be of typeTitledElement, orObservableField<TitledElement>if you want to listen to or notify of changes to the checked states. - (Optionally)
tools:itemCount="2"to get layout preview of 2 items in the Android Studio Designer
- A reference to your button list through the
- Expose a list of
CheckBoxViewModelobjects in your ViewModel - Add a
com.nilsenlabs.bindableradiobuttons.checkbox.BindableCheckBoxListto your view and wire up:- A reference to your checkbox list through the
app:checkboxesproperty - (Optional) A reference to a custom view XML layout through the
app:itemViewIdproperty. The file must contain one and only one view element which extendsCompoundButton - Use the
isChecked: ObservableBooleanfields of the ViewModel if you need to react to changes. - (Optional) Use
app:onCheckedChangedon the<BindableCheckBoxList>to listen for changes. Signature in the ViewModel:val checkedChanged = BindableCheckBoxList.CheckedChangeListener { viewModel -> }
- A reference to your checkbox list through the
- Expose a list of
ButtonViewModelobjects in your ViewModel (1) - Add a
com.nilsenlabs.bindableradiobuttons.buttons.BindableButtonListto your view and wire up:- A reference to your button list through the
app:buttonsproperty - (Optionally) A reference to a custom Button XML view through the
app:buttonViewIdproperty - (Optionally) A reference to a click listener using the
onButtonClickedproperty. However the most common approach would probably be wiring individual click listeners using theonClickevent of the individualButtonViewModel-classes.
- A reference to your button list through the
Example: See activity_main.xml and MainActivityViewModel
(1) If you don't want to use the provided ButtonViewModel, you can create your own button ViewModel extending ButtonViewModelInterface.
Same goes for CheckboxViewModelInterface for CheckBox and RadioButtonViewModelInterface for RadioButton.
The project has two modules: The library and an example app named sample. In the sample module you'll find
a fully functioning example. See especially these two files:
-
It does not support a mutating list in the sense that individual additions or removal of elements take effect. If you need to apply changes to the list, rebind the entire list.
-
It does not allow the text of the buttons to change dyanamically. Most buttons should not change title or function throughout their lifetime on their screen.
-
It does not allow you to databind individual properties of the views other than click listeners and text. This would require a different approach or at least a custom binding interface that takes away the simplicity of the library. If you do need to databind e.g. the color of each check box text, first ask yourself if you really do need that, then either build the support from scratch, fork this library or make a pull request.
