Reputation: 62189
I want to override a layout file from android namespace, e.g. R.layout.popup_menu_item_layout
(which is referenced from code as com.android.internal.R.layout.popup_menu_item_layout
). By saying override, I assume declaring an xml
file in the project which would be prioritized over the layout that framework owns.
Note, this is just an example layout, so the question concerns to each layout that's present in sdk/platforms/android-XX/data/res/layout
directory.
tools:override
There's an undocumented tools:override
tag available, which overrides specific resources. See this answer for an example, which overrides values from Design Support Library, not from Android framework.
Applying tools:override="true"
to the root tag of the layout won't take effect.
XML layout references - refs.xml
As described in this post, declaring a refs.xml
file in /values/
directory with following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="layout" name="activity_main">@layout/activity_second</item>
</resources>
will refer to activity_second.xml
once activity_main.xml
is used. There's an answer that suggests using this technique in order to substitute Snackbar
's layout.
This also won't take effect.
Is there any legitimate way to override/substitute a layout file from android package?
Upvotes: 34
Views: 10613
Reputation: 389
don't know your issue have fixed or not but simple solution for this is create new layout that is same layout name of framework (in this case is popup_menu_item_layout). Then go to android google source to copy xml content popup_menu_item_layout
So you can custom anything u want. But remember don't change any id of views.
Upvotes: 0
Reputation: 323
That's just not how it works.
If you use an SDK on your project(on any technologies), and you need to modify some behavior, you will tweak this SDK and after that, compile your project with this news customized version.
Trying to modify it at runtime is not a good idea.
You will face multiple issues (retro compatibility, security trigger, TREBLE incompatibility , dependency issue, etc)
Of course, none of this solutions is applicable for a public app.
Upvotes: 0
Reputation: 3089
I know this is an old question but I also wanted to override a library layout with my own, here's how I did it.
The layout in question was called design_bottom_navigation_item
In refs.xml I added the following:
<resources xmlns:tools="http://schemas.android.com/tools">
<item name="design_bottom_navigation_item" type="layout" tools:override="true">@layout/bottom_navigation_item</item>
</resources>
There are 4 parts to this which I'll explain.
You can do this with any resource type this way.
Upvotes: 21