Search code examples
androidandroid-layoutandroid-fragmentsandroid-activityandroid-gradle-plugin

How to call layout xml from another module


I am setting ContentView in MainActivity as setContentView(R.layout.activity_main);

I have other module in package as well, for that module I defined in gradle file apply plugin: 'com.android.library' This once also has res/layout/activity_main.xml

So can I call activity_main.xml from another module from MainActivity?


Solution

  • Yes you can, but your module must have another Android packageId (EDIT: just to make sure, you declare your packageId inside your Android manifest). So e.g. you have com.example.app for your app module and com.example.lib for your library. Then you can access your resource ids by com.example.app.R or com.example.lib.R. So you would have com.example.lib.R.layout.activity_main for your layout inside your library module.