DEV Community

Cover image for DataBindingUtil.inflate vs View Binding Inflate
Vincent Tsen
Vincent Tsen

Posted on • Edited on • Originally published at vtsen.hashnode.dev

3 2

DataBindingUtil.inflate vs View Binding Inflate

You can inflate your fragment views either with data binding or view binding. Which one should be used?

If you turn on dataBinding in your build.gradle, it is likely you use the "data binding layout".

As mentioned in previous article here, when "data binding layout" is used, viewBinding is created automatically. Thus, you don't need to explicitly set the viewBinding true in the build.gradle file.

So you have 2 ways to inflate your fragment views in your onCreateView() - data binding method and view binding method.

1. Data Binding Method

FragmenMainBinding is the view binding class. To inflate fragment view, you need to pass in the LayoutInflater, layoutId, parent ViewGroup, attachToParent flag.

val binding: FragmenMainBinding = DataBindingUtil.inflate(
    inflater, R.layout.fragment_main, container, false)
Enter fullscreen mode Exit fullscreen mode

Well, that is awesome! Let's look at the second method using View Binding

2. View Binding Method

It is even more simple! You just need to pass in one LayoutInflater parameter.

val binding = FragmentMainBinding.inflate(inflater)
Enter fullscreen mode Exit fullscreen mode

Which One is Better?

It is obvious that the second View Binding method is better. It can be used either with dataBinding true or viewBinding true in your build.gradle file.

So why and when DataBindingUtil.inflate() is needed then? Well, if you look at the official document here, it states that

Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.

My next question is in what scenario the layoutId is unknown? I don't have the answer because I don't have such use case, do you?

Summary

DataBindingUtil.inflate data binding method is unnecessary in most cases. The view binding inflate method should be used instead.


See Also

Originally published at https://vtsen.hashnode.dev.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay