Last active
July 29, 2020 07:52
-
-
Save FareesHussain/2520a95c7003c188c148d5b133404ad0 to your computer and use it in GitHub Desktop.
steps to get rid of findViewById() using kotlin-android-extensions
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
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