- Use a viewmodel to bind the data
- Bind the
Button
'sonClick
method toMainViewModel.handleClick()
- Use a custom
@BoundFunction
annotation to make the viewmodel code more self-documenting
Last active
February 27, 2023 22:31
-
-
Save n8ebel/c2fe662418863f30c409e93eb2cef42c to your computer and use it in GitHub Desktop.
Basic example of using databinding to handle button clicks
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" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
<variable | |
name="viewModel" | |
type="com.n8ebel.databindingsandbox.MainViewModel" /> | |
</data> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="@{viewModel.message}" | |
/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text='@{"And here we are binding a simple value like the number " + viewModel.number}' | |
/> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Click Me" | |
android:onClick="@{() -> viewModel.handleClick()}" | |
/> | |
</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
class MainActivity : AppCompatActivity(), ViewModelListener { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main).bindData { | |
viewModel = MainViewModel("hello", 5, this@MainActivity) | |
} | |
} | |
// region implements ViewModelListener | |
override fun showMessage(message: String) { | |
Toast.makeText(this, message, Toast.LENGTH_SHORT).show() | |
} | |
// endregion implements ViewModelListener | |
} |
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
class MainViewModel(msg:String, num:Int, val listener:ViewModelListener) { | |
val message = ObservableField<String>(msg) | |
val number = ObservableInt(num) | |
@BoundFunction | |
fun handleClick() { | |
listener.showMessage("You Clicked It!!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment