ViewBinding and Databinding

https://developer.android.com/topic/libraries/view-binding/migration#groovy

https://developer.android.com/topic/libraries/view-binding#fragments

https://developer.android.com/topic/libraries/data-binding

key to remember

ProgressDialogBinding this name has come from the layout progressdialog.xml or progress_dialog.xml or progress_Dialog.xml or progressDialog.xml in my case it was progress_dialog.xml

val progressDialogBinding=ProgressDialogBinding.inflate(LayoutInflater.from(context))
or 
val progressDialogBinding=ProgressDialogBinding.bind(binding.root) 

You can use this in fragments or activities especially for the include layout when no id is given, the best is you give id to it. It’s easy and handy

bind will return null so handle when that view is not present in some layout but present in most example base fragment class.

while giving id give camel case name only don’t use _ this it will make life easy.

In fragment viewbinding

class ProgressDialog : DialogFragment() {
    private var _binding:ProgressDialogBinding?=null
    private val binding get() = _binding!!
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        _binding= ProgressDialogBinding.inflate(inflater, container, false)
        return binding.root
    }

 override fun onDestroyView() {
        super.onDestroyView()
        _binding=null
    }
}

By navalkishorjha

Leave a comment