Questions tagged [android-viewbinding]

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class 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. In most cases, view binding replaces findViewById.

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class 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.

In most cases, view binding replaces findViewById.

You can see this link for more.

482 questions
5
votes
4 answers

Adding buildFeatures { viewBinding true } results in "Cannot find a setter for <... android:visibility> that accepts parameter type 'int'

I want to start using viewBinding in our project but the mere addition of the configuration results in a compile error: android { buildFeatures { dataBinding true viewBinding true // new line and only change } results in: e:…
Giszmo
  • 1,944
  • 2
  • 20
  • 47
5
votes
1 answer

Renaming ID of a layout element does not rename the viewBinding reference

Lets say that I have the a layout named activity_main.xml and it has an element: An activity named MainActivity.java. In…
Tayyab Mazhar
  • 1,560
  • 10
  • 26
5
votes
1 answer

Possible to mock/test Android ViewBinding interactions?

currently trying to test the interactions with ViewBinding classes in Unit Tests "Invalid Input" should { "disable the LoginButton" { val viewBinding: FrLoginBinding = mockk() InvalidInputViewStateBinder.bind(InvalidInput,…
Flo We
  • 325
  • 2
  • 12
5
votes
1 answer

Custom Annotation like Butterknifes "@OnClick" for android viewBinding

I migrated a project from butterknife to android viewBinding. In Butterknife there is something like: @OnClick(R.id.button) fun onButtonClicked() And now I have to set all the button click listener like this: binding.button.setOnClickListener {…
Punika
  • 354
  • 3
  • 16
5
votes
1 answer

Using View Binding displays blank white screen

I was experimenting View Binding in my new app. Suddenly today when I run the app it displays blank white screen instead of UI. If I remove binding and use setContentView, it works! Any idea why this is happening? Code: build.gradle viewBinding { …
5
votes
2 answers

NullPointerException when using Kotlin ViewBinding inside Fragment

I'm trying to add a click listener to a button inside my fragment using kotlin view binding. I am setting the click listener in the onCreateView method. When I do this I get a null pointer exception since the button is not created yet. I thought the…
dwvldg
  • 293
  • 4
  • 12
4
votes
2 answers

Viewbinding within Android and Exoplayer

I'm using Android Exoplayer in one of my Fragment. Within Exoplayer I use a custom control layout"@layout/custom_player" for the controls. I have different elements within the layout for example I have a button element"optionBtn" which I want to…
Riccoh
  • 359
  • 1
  • 4
  • 19
4
votes
1 answer

Where is better to use View Binding in Fragments? (onCreateView vs onViewCreated)

I saw some examples where binding were defined and used in onCreateView() using inflate() and in onViewCreated() using bind(). What's the difference? And where is it better to operate with our views(RecyclerView, TextView and so on)? Google…
MrArtyD
  • 113
  • 1
  • 9
4
votes
1 answer

Do I need to clear/null my view binding field in custom views?

I've started using View Binding in my fragments and activities. The recommended pattern for Fragments is to null the binding field in onDestroyView(). I'm also using view binding in my custom views. Do I need to handle clearing that view binding…
szaske
  • 1,887
  • 22
  • 32
4
votes
1 answer

How to access view from included layout with kotlin synthetic in android

In my fragment layout I have added a layout with include
WISHY
  • 11,067
  • 25
  • 105
  • 197
4
votes
1 answer

ViewBinding abstract class or interface

Is there any way to make an abstract class or interface for ViewBinding. Maybe I will show a simple example of what I exactly mean. Let's say that I have two fragments. Both have TextView named universalTextView. In fragment classes I have value…
iknow
  • 8,358
  • 12
  • 41
  • 68
4
votes
1 answer

Why is MaterialAlertDialog so slow when measuring a custom view/layout?

I'm working on a hobby project where I'm building a custom Bluetooth remote that can be configured using a companion native Android app. Everything is working just fine but I'm running into this weird performance issue with the MaterialAlertDialog…
4
votes
2 answers

Missing required view with ID with view binding and navigation component

I am trying to migrate my project to view binding and I get an exception when I start my app. My main activity contains a NavHostFragment like so:
Sebastien
  • 3,583
  • 4
  • 43
  • 82
4
votes
1 answer

onClick function not working with View Binding Android Kotlin

I am a beginner in kotlin, trying to develop sample projects. I tried using View.OnClickListener interface to get Id of any view clicked in my layout. I have 9 buttons in my layout (tic tac toe game) but when the code is run nothing happens on…
Milan Jayan
  • 96
  • 1
  • 6
4
votes
1 answer

ViewBinding in a Dialogfragment using a customview

I have a question about using viewBinding in a dialog fragment, that uses a custom view. Is there a standard way of doing this? My code that uses findViewById() for the dialog fragment but I would like to use viewbinding as that is standard across…