Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 237
user2661518 Avatar asked Apr 15 '15 19:04

user2661518


People also ask

Is it possible to include one layout definition in another?

To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

What an XML layout file contains?

In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.

How do you merge on Android?

Under <merge/> tags, we can specify a part of the layout that has to come in the main layout. It is similar to the main layout having Button, TextView, etc., It can be specified in a meaningful android naming convention XML file. eg: custom_layout.


1 Answers

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.

like image 191
einschnaehkeee Avatar answered Sep 23 '22 17:09

einschnaehkeee