4

I have two modules, common and application1.

common includes toolbar.xml, (which contains a Button) and application1 includes MainActivity and activity_main.

In activity_main.xml, I want to include the toolbar which I declared in common. However, when I try to access the Button in the toolbar from MainActivity, I get an error

Cannot access 'com.application1.databinding.ToolbarBinding'. Check module classpath for missing or conflicting dependencies

Does anyone know how to fix this? I've implemented the 'common' in gradle, but it still doesn't work. Below is my setup:

application1.gradle

dependencies {
    implementation project(':common')
}

common/res/layout/toolbar.xml

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</androidx.appcompat.widget.Toolbar>

application1/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar"
        android:layout="@layout/toolbar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                 
        android:text="Some Random Text" />

</LinearLayout>

application1/java/MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        mBinding.toolbar.button.setEnabled(true) // Cannot access 'databinding.ToolbarBinding'
        setContentView(mBinding.root)
    }

}       
13
  • Please show toolbar layout? Commented Jul 9, 2020 at 16:43
  • you can access mBinding.toolbar.button.setEnabled(true) Commented Jul 9, 2020 at 16:45
  • I can't access mBinding.toolbar.button because it's not in the same module. That's the whole reason why I made this post Commented Jul 9, 2020 at 18:28
  • did you get solution for this? Commented Jul 10, 2020 at 4:32
  • not yet.this is actually really important so i might place a bounty on it Commented Jul 10, 2020 at 13:15

3 Answers 3

2

How to access include element from another module with Databinding?

We need to do the below points for getting a layout and binding from another module:

  1. The module should be an Android Library module

  2. This lib module should be mentioned in the app Gradle file inside dependency as

    dependencies {
          api project(':mylibrary')
      }
    
  3. Both the app module and library module gradle file should mention the databinding as,

     buildFeatures {
         dataBinding true
     }
    

Then in your both xml, do put the tag for binding the layout. So both the xml will be as below:

  1. The library module XML:

     <layout>
     <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
         <Button
             android:id="@+id/button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
    
     </androidx.appcompat.widget.Toolbar>
     </layout>
    
  2. The app module XML:

      <layout>
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
           <include
             android:id="@+id/toolbar"
             layout="@layout/toolbar" />
    
           <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"                 
             android:text="Some Random Text" />
    
        </LinearLayout>
     </layout>
    

Then, in your activity you can simply access the button inside the library as below:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.toolbar.button // you can access now
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I've done everything in your answer - My module is already a Android Library, I've already added common as a dependency, I've enabled databinding on both modules, and put tags on relevant elements, but it still doesn't work for me :(
I don't thinkg it's a good idea to have the included layout have id toolbar and the root of the included layout have the same id toolbar. At best it makes debugging harder, and it may be the reason it gets confused.
1

Many times databinding goes crazy on my laptop and caches old code. I have this going on for maybe a year, on many kotlin, android studio and gradle versions. When it happens I usually do one or all of the following:

./gradlew clean assembleDebug ./gradlew clean assembleDebug --no-build-cache

After the command line compiles fine Android Studio is back working.

Maybe not specific to your problem, when those don't do the job I do ./gradlew assembleDebug --stacktrace and carefully read the errors from bottom to top.

Of course, you also have to wrap both layout files with <layout> and have databinding enabled in both layouts.

If you want to access the View objects instead of doing databinding, I'd recommend taking a look at https://developer.android.com/topic/libraries/view-binding which is a recent implementation and probably better quality than databinding, which is quite hard to work with when any error comes out of it.

Comments

0

You can find the info on how to link the library here:

LINK ANDROID LIBRARY

You need to use with the new gradle:

 implementation(project(":example-library"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.