https://github.com/jrvansuita/view-binding
🖇 This is an Android library. It makes your life easier by reducing boilerplate code and avoiding typecasting and redundant lines of code when using View Binding
https://github.com/jrvansuita/view-binding
android viewbinding viewbinding-property-delegate
Last synced: 25 days ago
JSON representation
🖇 This is an Android library. It makes your life easier by reducing boilerplate code and avoiding typecasting and redundant lines of code when using View Binding
- Host: GitHub
- URL: https://github.com/jrvansuita/view-binding
- Owner: jrvansuita
- License: mit
- Created: 2023-01-18T19:55:43.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-20T09:02:23.000Z (over 3 years ago)
- Last Synced: 2025-08-22T03:48:59.575Z (11 months ago)
- Topics: android, viewbinding, viewbinding-property-delegate
- Language: Kotlin
- Homepage:
- Size: 231 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README

# View Binding
This is an [**Android**](https://developer.android.com) library. It makes your life easier by reducing boilerplate code and avoiding redundant lines of code when are using the [ViewBinding](https://developer.android.com/topic/libraries/view-binding) feature on your Android project. This library show up in my head as an very good example on how the property delegation functionality from kotlin can help us.
-----
## Usage on Activity
Binding
> Yes, the ```viewBinding()``` method will bind to the root view for you.
```Kotlin
class MainActivity : AppCompatActivity(R.layout.activity_main) {
private val binding : ActivityMainBinding by viewBinding()
}
```
Inflating
> You don't need to set the layout calling ```setContentView()```. It will be done automatically.
```Kotlin
class MainActivity : AppCompatActivity() {
private val binding : ActivityMainBinding by viewBinding(ViewBindingMethod.INFLATE)
}
```
## Usage on Fragment
Binding
> The ```viewBinding()``` method will track the fragment view lifecycle and destroy the ViewBinding instance for you.
```Kotlin
class MyFragment : Fragment(R.layout.fragment_layout) {
private val binding by viewBinding()
}
```
Inflating
> Using this way, it gives you the ```onCreateView```
```Kotlin
class MyFragment : Fragment() {
private val binding by viewBinding(ViewBindingMethod.INFLATE)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = binding.root
}
```
## Installation
Add the following dependency to your `build.gradle` file:
```groovy
dependencies {
implementation 'com.github.jrvansuita:view-binding:1.0.2'
}
```
> Don't forget to add the JitPack maven repository to the list of repositories: `maven { url "https://jitpack.io" }`