Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View binding does not have access to fragment

I have an activity with ConstraintLayout and fragment on it:

<androidx.constraintlayout.widget.ConstraintLayout 
 ...attributes....
>
  <fragment
     android:id="@+id/containerView"
     app:navGraph="@navigation/some_navigation"
     ...other attributes...
   />

  ...other views...

I'm using view binding in the activity code:

  binding = ActivityMainBinding.inflate(layoutInflater)

the issue here that i have no access to the binding.containerView. Does in case of fragment I should use findViewById ?

like image 374
Siarhei Avatar asked Jan 24 '23 08:01

Siarhei


1 Answers

Fragment tag ( <fragment>) is not a view it act as a container for other views, So you cannot access it like other views...

You will get compilation error when you try to access a non-view element using binding

Instead you can use,

supportFragmentManager.findFragmentById(R.id.containerView)
like image 182
CodeFreak Avatar answered Feb 04 '23 03:02

CodeFreak