Mallow's Blog

View Binding on Android

View binding is a feature that allows you to write code more easily that communicate with views and replace findViewById with generated binding objects to simplify code, remove bugs, and avoid all the boilerplate of findViewById.

View binding is available in Android Studio 3.6

Setup instructions:- 

1. Enable view binding in a module, add the ViewBinding element to its build.gradle file (no libraries dependencies). As shown in the following example:

android {
    // Enable the view binding
    viewBinding {
        enabled = true
    }
}

2. Generates a binding object for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an Id in the corresponding layout as shown in the following example:
(activity_login —> ActivityLoginBinding)

3. Complete support for both Java programming language and Kotlin Programming Language

Use view binding in an Activity:

To set up an instance of the binding class for use with an activity, perform following steps in the activity’s onCreate( ) method: 

1. Call the static method of inflate( ) included in the generated view binding class, An instance of the binding class for the activity to use.

2. Get a reference to the root view by either calling the getRoot( ) method. 

3. Pass the root view to setContentView ( ) to make it the active view on the screen.


private lateinit var binding: ActivityLoginBinding

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


You can use the instance of the binding class to refer the views:


binding.username.afterTextChanged {
    loginViewModel.loginDataChanged(
        username.text.toString(),
        password.text.toString()
    )
}

binding.password.apply {
    afterTextChanged {
        loginViewModel.loginDataChanged(
            username.text.toString(),
            password.text.toString()
        )
    }

binding.login.setOnClickListener {
    loading.visibility = View.VISIBLE
    loginViewModel.login(username.text.toString(), password.text.toString())
}


Use view binding in a Fragment:

To set up an instance of the binding class for use with an fragment, perform following steps in the fragment onCreateView( ) method:

1. Call the static method of inflate( ) included in the generated view binding class, An instance of the binding class for the fragment to use.

2. Get a reference to the root view by either calling the getRoot( ) method. 

3. Return the root view to onCreateView ( ) to make it the active view on the screen.


private lateinit var binding: LoginFragmentBinding

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    binding = LoginFragmentBinding.inflate(inflater, container, false)

    return binding.root
}


You can use the instance of the binding class to refer the views:


binding.username.afterTextChanged {
    loginViewModel.loginDataChanged(
        username.text.toString(),
        password.text.toString()
    )
}

binding.password.apply {
    afterTextChanged {
        loginViewModel.loginDataChanged(
            username.text.toString(),
            password.text.toString()
        )
    }

binding.login.setOnClickListener {
    loading.visibility = View.VISIBLE
    loginViewModel.login(username.text.toString(), password.text.toString())
}


Differences from findViewById:-

View binding has important advantages over using findViewById

Null safety: View binding creates direct references to view’s, This mean that there’s no risk of a null pointer exception due to an invalid view ID. When a view is only present in some configurations of a layout file , the field containing its reference in the binding class is marked with @Nullable.

Type safety: Each binding class have types matching the views they referred in the XML layout file. This means that there’s no risk of a class cast exception. 

Comparison with data binding:- 

View binding and data binding both will be generate binding classes that can use to refer views directly. View binding is intended to handle simple use cases and provides the following advantage over data binding: 

Faster compilation: View binding requires no annotation processing, so compile times are faster. 

Ease of use: View binding doesn’t require specially-tagged XML layout files, so faster to adopt in your application. Once enable view binding in a module, it applies to all of that module’s layouts automatically generate. 

Conclusion:- 

View binding solved some previous issues like :

  • To avoid the Null pointer exception ( due to an invalid view id)
  • To avoid Class casting exception (due to miss match in different types of views)
  • Improving the speed and performance of our application development.

– Manikandan K,
Android Development Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: