{"id":17238362,"url":"https://github.com/hoc081098/viewbindingdelegate","last_synced_at":"2025-04-05T15:08:47.391Z","repository":{"id":38025013,"uuid":"285541782","full_name":"hoc081098/ViewBindingDelegate","owner":"hoc081098","description":"Simple one-liner ViewBinding in Fragments and Activities with Kotlin 🍄 Simplify usage of Android View Binding with Kotlin Property Delegates and solve behavior of Fragment’s ViewLifecycleOwner 🌱 ViewBindingPropertyDelegate","archived":false,"fork":false,"pushed_at":"2025-03-12T17:25:22.000Z","size":5019,"stargazers_count":115,"open_issues_count":12,"forks_count":16,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T14:11:24.408Z","etag":null,"topics":["android-view-binding-delegate","android-viewbinding-delegate","android-viewbing","androidviewbinding","view-binding","view-binding-delegate","viewbinding","viewbinding-delegate","viewbinding-property-delegate","viewbindingdelegate","viewbindingpropertydelegate"],"latest_commit_sha":null,"homepage":"https://jitpack.io/#hoc081098/ViewBindingDelegate","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hoc081098.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-06T10:28:59.000Z","updated_at":"2024-11-06T22:33:18.000Z","dependencies_parsed_at":"2023-12-13T21:54:36.476Z","dependency_job_id":"93a22578-4776-4572-a017-faccfd4eacab","html_url":"https://github.com/hoc081098/ViewBindingDelegate","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2FViewBindingDelegate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2FViewBindingDelegate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2FViewBindingDelegate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2FViewBindingDelegate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoc081098","download_url":"https://codeload.github.com/hoc081098/ViewBindingDelegate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353746,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-view-binding-delegate","android-viewbinding-delegate","android-viewbing","androidviewbinding","view-binding","view-binding-delegate","viewbinding","viewbinding-delegate","viewbinding-property-delegate","viewbindingdelegate","viewbindingpropertydelegate"],"created_at":"2024-10-15T05:45:25.361Z","updated_at":"2025-04-05T15:08:47.367Z","avatar_url":"https://github.com/hoc081098.png","language":"Kotlin","funding_links":["https://www.buymeacoffee.com/hoc081098"],"categories":[],"sub_categories":[],"readme":"# ViewBindingDelegate\n\n## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)\n\n- Simplify usage of Android View Binding with Kotlin Property Delegates and solve behavior of Fragment’s ViewLifecycleOwner.\n- Simple one-liner ViewBinding in Fragments and Activities with Kotlin.\n- Lightweight and simple library.\n\n![Month](https://jitpack.io/v/hoc081098/ViewBindingDelegate/month.svg)\n![Week](https://jitpack.io/v/hoc081098/ViewBindingDelegate/week.svg)\n![Android Tests](https://github.com/hoc081098/ViewBindingDelegate/workflows/Android%20Tests/badge.svg)\n![Android build CI](https://github.com/hoc081098/ViewBindingDelegate/workflows/Android%20build%20CI/badge.svg)\n[![Jitpack](https://jitpack.io/v/hoc081098/ViewBindingDelegate.svg)](https://jitpack.io/#hoc081098/ViewBindingDelegate)\n[![GitHub](https://img.shields.io/github/license/hoc081098/ViewBindingDelegate?color=4EB1BA)](https://opensource.org/licenses/MIT)\n[![ktlint](https://img.shields.io/badge/code%20style-%E2%9D%A4-FF4081.svg)](https://ktlint.github.io/)\n[![Kotlin](https://img.shields.io/badge/kotlin-1.7.10-blue.svg?logo=kotlin)](http://kotlinlang.org)\n[![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14)\n[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fhoc081098%2FViewBindingDelegate\u0026count_bg=%2379C83D\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=hits\u0026edge_flat=false)](https://hits.seeyoufarm.com)\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"logo.png\" height=\"300\"/\u003e\n\u003c/p\u003e\n\nLiked some of my work? Buy me a coffee (or more likely a beer)\n\n[![\"Buy Me A Coffee\"](https://cdn.buymeacoffee.com/buttons/default-orange.png)](https://www.buymeacoffee.com/hoc081098)\n\n\n## Medium article\n\nRead [ViewBinding Delegate — one line](https://hoc081098.medium.com/viewbinding-delegate-one-line-4d0cdcbf53ba) to get details about implementation.\n\n## Getting Started\n\n### 1. Add the JitPack repository to your build file\nAdd it in your root build.gradle at the end of repositories:\n\n- Kotlin\n```kotlin\nallprojects {\n  repositories {\n    ...\n    maven(url = \"https://jitpack.io\")\n  }\n}\n```\n\n- Groovy\n```gradle\nallprojects {\n  repositories {\n    ...\n    maven { url 'https://jitpack.io' }\n  }\n}\n```\n\n### 2. Add the dependency\n\n```kotlin\ndependencies {\n  implementation(\"com.github.hoc081098:ViewBindingDelegate:1.4.0\")\n}\n```\n\n### 3. Enable ViewBinding\n\nhttps://developer.android.com/topic/libraries/view-binding#setup\n\n## Usage\n\n```kotlin\nimport com.hoc081098.viewbindingdelegate.*\n```\n\n### 1. Activity (with reflection). [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/MainActivity.kt)\n\n\u003cdetails\u003e\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nclass MainActivity : AppCompatActivity(R.layout.activity_main) {\n  private val viewBinding by viewBinding\u003cActivityMainBinding\u003e()\n\n  override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n\n    viewBinding.button.setOnClickListener {\n      startActivity(Intent(this@MainActivity, SecondActivity::class.java))\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 2. Activity (without reflection): Pass `::bind` method reference. [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/SecondActivity.kt)\n\n\u003cdetails\u003e\n\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nclass SecondActivity : AppCompatActivity(R.layout.activity_second) {\n  private val binding by viewBinding(ActivitySecondBinding::bind)\n\n  override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    binding.root\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 3. Fragment (with reflection). [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/ReflectionFragment.kt)\n\n\u003cdetails\u003e\n\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nclass FirstFragment : Fragment(R.layout.fragment_first) {\n  private val binding by viewBinding\u003cFragmentFirstBinding\u003e {\n    button.setOnClickListener(null)\n  }\n\n  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n    super.onViewCreated(view, savedInstanceState)\n\n    binding.button.setOnClickListener {\n      findNavController().navigate(R.id.actionFirstFragmentToSecondFragment)\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 4. Fragment (without reflection): Pass `::bind` method reference. [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/NotReflectionFragment.kt)\n\n\u003cdetails\u003e\n\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nclass SecondFragment : Fragment(R.layout.fragment_second) {\n  private val binding by viewBinding(FragmentSecondBinding::bind)\n\n  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n    super.onViewCreated(view, savedInstanceState)\n    binding.root\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 5. Includes `\u003cmerge/\u003e` tag layout: Create 2 `ViewBinding` property. [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/Reflection2Fragment.kt)\n\n\u003cdetails\u003e\n\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n\n```kotlin\nclass ThirdFragment : Fragment(R.layout.fragment_third) {\n  private val includeBinding by viewBinding\u003cFragmentThirdIncludeBinding\u003e()\n  private val binding by viewBinding\u003cFragmentThirdBinding\u003e { buttonThird.setOnClickListener(null) }\n\n  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n    super.onViewCreated(view, savedInstanceState)\n\n    includeBinding.textViewThirdInclude.text = \"Working...\"\n    binding.buttonThird.setOnClickListener {\n      Toast.makeText(requireContext(), \"Clicked\", Toast.LENGTH_SHORT).show()\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 6. The `Dialog` of `DialogFragment`[See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/DemoDialogFragment.kt)\n\nExtends `DefaultViewBindingDialogFragment` or implements `ViewBindingDialogFragment`.\n\n\u003cdetails\u003e\n\n  \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nclass DemoDialogFragment : DefaultViewBindingDialogFragment() {\n  private val viewBinding by dialogFragmentViewBinding(R.id.root, DialogFragmentDemoBinding::bind)\n  private val viewBinding2 by dialogFragmentViewBinding\u003cDialogFragmentDemoBinding\u003e(R.id.root)\n\n  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {\n    return AlertDialog.Builder(requireContext())\n      .setTitle(\"Demo dialog\")\n      .setNegativeButton(\"Cancel\") { _, _ -\u003e }\n      .setPositiveButton(\"OK\") { _, _ -\u003e }\n      .setView(R.layout.dialog_fragment_demo)\n      .create()\n  }\n\n  override fun onResume() {\n    super.onResume()\n\n    viewBinding.textInputLayout\n    viewBinding2.textInputLayout\n  }\n}\n```\n\n\u003c/details\u003e\n\n### 7. `inflateViewBinding` extension methods on `ViewGroup`/`LayoutInflater`/`Context`. [See example](https://github.com/hoc081098/ViewBindingDelegate/blob/master/app/src/main/java/com/hoc081098/example/DemoAdapter.kt)\n\nCan be used in `RecyclerView.Adapter # onCreateViewHolder` to easily create a `RecyclerView.ViewHolder` with a `ViewBinding`.\n\n\u003cdetails\u003e\n    \u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```kotlin\nimport com.hoc081098.viewbindingdelegate.inflateViewBinding\n\nclass DemoAdapter : ListAdapter\u003cString, DemoAdapter.VH\u003e(...) {\n  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = VH(parent inflateViewBinding false)\n\n  override fun onBindViewHolder(holder: VH, position: Int) = holder.bind(getItem(position))\n\n  class VH(private val binding: ItemRecyclerBinding) : RecyclerView.ViewHolder(binding.root) {\n    fun bind(item: String) { ... }\n  }\n}\n```\n\n\u003c/details\u003e\n\n## Note\n\n### 1. Activity\n  Must `setContentView` before access `ViewBinding` property. This can be done easily with `constructor`:\n  ```java\n  public AppCompatActivity(@LayoutRes int contentLayoutId) { ... }\n  ```\n  ```kotlin\n  class MainActivity : AppCompatActivity(R.layout.activity_main) { ... }\n  ```\n\n### 2. Fragment\n  `Fragment`'s `View` must be not null before access `ViewBinding` property. This can be done easily with `constructor`:\n  ```java\n  public Fragment(@LayoutRes int contentLayoutId) { ... }\n  ```\n  ```kotlin\n  class FirstFragment : Fragment(R.layout.fragment_first) { ... }\n  ```\n\n### 3. Proguard\nIf there is any problem with `Proguard`, add below to your `app/proguard-rules.pro`:\n```\n# ViewBindingDelegate uses Reflection.\n-keepclassmembers class ** implements androidx.viewbinding.ViewBinding {\n    public static ** bind(android.view.View);\n\n    public static ** inflate(android.view.LayoutInflater, android.view.ViewGroup, boolean);\n\n    public static ** inflate(android.view.LayoutInflater, android.view.ViewGroup);\n}\n```\n\n### 4. Throws `IllegalStateException`: \"Attempt to get view binding when fragment view is destroyed\" when accessing delegate property in `onDestroyView`\n\nSince version `1.0.0-alpha03 - Feb 16, 2021`, we cannot access ViewBinding delegate property in `onDestroyView` (this causes many problems). Recommended way is passing a lambda to `onDestroyView: (T.() -\u003e Unit)? = null` parameter of extension functions, eg.\n\n```diff\n- private val binding by viewBinding\u003cFragmentFirstBinding\u003e()\n\n+ private val binding by viewBinding\u003cFragmentFirstBinding\u003e { /*this: FragmentFirstBinding*/\n+   button.setOnClickListener(null)\n+   recyclerView.adapter = null\n+ }\n\n  override fun onDestroyView() {\n    super.onDestroyView()\n-   binding.button.setOnClickListener(null)\n-   binding.recyclerView.adapter = null\n  }\n```\n\n### 5. Min SDK version\n\nSince version `1.2.0`, `minSdkVersion` has been changed to `14`.\n\n## License\n\n    MIT License\n\n    Copyright (c) 2020-2022 Petrus Nguyễn Thái Học\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoc081098%2Fviewbindingdelegate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoc081098%2Fviewbindingdelegate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoc081098%2Fviewbindingdelegate/lists"}