Skip to content

Instantly share code, notes, and snippets.

@FareesHussain
Last active July 29, 2020 07:52
Show Gist options
  • Save FareesHussain/2520a95c7003c188c148d5b133404ad0 to your computer and use it in GitHub Desktop.
Save FareesHussain/2520a95c7003c188c148d5b133404ad0 to your computer and use it in GitHub Desktop.
steps to get rid of findViewById() using kotlin-android-extensions
Steps
1. add the following in build.gradle(app)
android{
..
buildFeatures{
viewBinding true
}
}
2. inside the layout tag in the xml file add
<LinearLayout
..
tools:viewBindingIgnore="false"
>
..
</LinearLayout>
3. (binding view)
(a) in Activities(MainActivity.xml)
class MainActivity : AppCompatActivity(){
private lateinit var binding : ActivityMainBinding
..
override fun onCreate(savedInstanceState:Bundle){
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main) ------ comment this and add the following
//__________
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
//__________
..
}
..
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment