different between call view by name or use binding in android

i'am a newest in android kotlin

I want to know what is the difference between the two lines of code below and which one is better to use

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMaindinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    bindins = DataBindingutil.setContentview( this, R.layout.activity_main)


        textview.text="text"//or

        binding.textview.text="text"
}}

1 answer

  • answered 2022-05-07 19:04 Tyler V

    TL;DR There are a few ways of getting views - using view binding is the currently recommended method.

    View Binding (recommended)

    The recommended way of accessing views is to use view binding. It offers a few advantages over getting the views manually

    • Less boilerplate code (you don't need to write a bunch of findViewById calls)
    • Null safety (catches errors like trying to access views from the wrong layout at build time instead of run time)
    • Type safety - the type of each view is set to help avoid invalid casts
    class MainActivity : AppCompatActivity() {
        private lateinit var binding: ActivityMainBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    
            // or, without using DataBindingUtil
            // binding = ActivityMainBinding.inflate(layoutInflater)
            // setContentView(binding.root)
    
            binding.textview.text="text"
        }
    }
    

    Manually Getting Views

    The old way also still works - you can always call findViewById yourself. Keep in mind that it may error or return null if the view is not found or if you try to cast it to the wrong type.

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
            val textview = findViewById<TextView>(R.id.textview)
            textview.text="text"
        }
    }
    

    Kotlin Synthetics (deprecated)

    Kotlin synthetics were introduced in 2017 to make it easier to get views, but are now deprecated and should be replaced with view binding. With synthetics you would have an import with your layout file name, then you could just access views from it directly.

    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            
            textview.text="text"
        }
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum