Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Convert FindViewById to Binding in android ?

By M.K. Verma · 4 May 2022

Convert FindViewById to Binding in android ?

Using the binding structure makes more sense now and saves you a lot of code. eg:if the activity was called HomeActivity it would be ActivityHomeBinding

build.gradle(module)

buildFeatures {
        viewBinding true
        dataBinding true
    }

MainActivity

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.apply {
         //eg:
         button.setOnClickListener{
           }
        }
    }