Simple one-liner ViewBinding in Fragments and Activities with Kotlin

android {
viewBinding {
enabled = true
}
// main_activity.xml<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello ViewBinding!"/>
</FrameLayout>
/* Copyright (C) 2020 The Android Open Source Project */
class MainActivity: AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.button.onClick {
showToast("hello world!")
}
/* Copyright (C) 2020 The Android Open Source Project */
class FirstFragment : Fragment(R.layout.first_fragment) {
// Scoped to the lifecycle of the fragment's view (between onCreateView and onDestroyView)
private var fragmentFirstBinding: FragmentFirstBinding? = null

override fun
onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentBlankBinding.bind(view)
fragmentFirstBinding = binding
binding.button.onClick {
showToast("Hello binding!")
}
}

override fun onDestroyView() {
// Consider not storing the binding instance in a field
// if not needed.
fragmentBlankBinding = null
super
.onDestroyView()
}
}

How to actually make it a one-liner

implementation "androidx.lifecycle:lifecycle-runtime:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.Zhuinden:fragmentviewbindingdelegate-kt:1.0.0'
}
inline fun <T : ViewBinding> AppCompatActivity.viewBinding(
crossinline bindingInflater: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
bindingInflater.invoke(layoutInflater)
}
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(MainActivityBinding::inflate)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)

binding.button.onClick {
showToast("hello world!")
}
class FirstFragment: Fragment(R.layout.first_fragment) {
private val binding by viewBinding(FirstFragmentBinding::bind)

override fun onViewCreated(view: View, bundle: Bundle?) {
super.onViewCreated(view, bundle)

binding.buttonPressMe.onClick {
showToast("Hello binding!")
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabor Varadi

Gabor Varadi

Android dev. Zhuinden, or EpicPandaForce @ SO. Extension function fan #Kotlin, dislikes multiple Activities/Fragment backstack.