Let's say i have a layout with style:
<LinearLAyout ... >
<TextView ... style=someStyle />
<ImageView ... style=someImageStyle ... />
</LinearLayout>
the style will be defined in an xml in my project.
How can i override that style with an external xml ? (i'm asking because i've noticed the View does not have applyStyle\setStyle or anything of that sort (best bet, because style need to be parsed, compared against android:attr for validation and then applied on each item of the view).
I do wonder how am i suppose to make downlaodable themes for my app.
After some experience i can share my thoughts about it here in my own question...
So first remark is, even in your own styles, use Dimentions and colors and attributes as much as you can. this way if you ever want to programatically apply a style to a view you can use getResources.getDimen... , getRessources().getColor(), getResrouces.getDrawable() etc...
In addition you can take the code from the aapt or open an existing apk and take the compiled xml from there and then use the same code reading the xml (it's C code mind you!!!) from android source. i would not do it from the simple reason it's an overkill to apply a simple style to a view.
The time it takes to write the method is too short. and if you must do it in xml, you can create an xml file with just your view and then inflate it, you can't replace the style in run time though. for that you have to define your own mechanism to replace colors, sizes, backgrounds etc... and supply the images as well, which is not so easy, reading those images from the local storage, if i'm not mistaken is less productive then readin them from assets or drawable directories.
Related
Problem
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.
What I've tried
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.
Question
Is there any legitimate way to override/substitute a layout file from android package?
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.
Name: This is the name of the layout you want to override
Type: The type of resource you are trying to override, in this case a layout.
tools:override: This is how you tell Android Studio to override the library layout with your own.
Value: This is where you specify what resource you want to use instead.
You can do this with any resource type this way.
What is that you're trying to do?
If the idea to only replace how the menu-item will look like, you can try the following:
Create a custom MyMenuAdapter extends MenuAdapter
Override the getView method to return the view from your adapter.
You are trying to customise your sdk on the application itself, at runtime.
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)
You have 4 possibilities to do what you want:
Make your own android rom where you will apply your modification
Copy the resources you need to modify on a fake xmlObject with the tag, after the onPostCreate of your application, you will be able to modify the when inflation. You can generalize this behavior and it will simulate an sdk overlay.
Make your own sdk :)
Multi-level reflection, but, no way you succeed with a stable version
Of course, none of this solutions is applicable for a public app.
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.
I know that we can have different layout files for supporting different screen sizes in Android.
Does anyone know if there is an option to change all other layout files when I make changes to the original layout file? For example, say I have a layout file - main.xml under layout, layout-large, layout-sw600dp and layout-sw720dp directories. If I make some changes to the main.xml in the layout directory, is there any setting which would automatically make that change in the other layout directories as well?
For the formatting capabilities I use an answer for this. This refers to the comment above and elaborates on Aleksey's answer.
<include
android:id="#+id/some_id_if_you_nee_one"
layout="#layout/some_other_xml_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
You may add any additional (orientation or resolution specific) formatting like below the layout= statment.
Everything that is not specific to the current resolution/orientation and common for all resolutions/oriantations should go into some_other_xml_file.xml
This works for all full views and subclasses of view. If you want something similar only for groups of style attributes then you can start a styles.xml and refer to the styles with style="..." statements.
The answer is no. And i'm not sure there is such tool. Exceptions: naming of params (strings, drawables, etc.).
Some hint: compose layouts from small parts, that inserted using
<include />.
So when you change small parts – all layout changes (not sure with different sw-*** layouts, but in one folder it works).
Can anyone explain the question mark means in Android XML attributes?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.
\? escapes the question mark.
The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.
So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different #drawable resource, say robot.png or hobbit.png, in each theme definition.
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
To support different resolutions, we need to make variations of layout files as described in Supporting Multiple Screens very well. Assuming you don't plan to show different arrangement of your UI but just want to stretch appropriately, your variations would be mostly about different weights. At least that is the case with my app so far.
Now, how do you manage changing the application with this structure? Since it repeats the layout many times, one layout change in your application causes multiple files change.
I thought of two options:
Changing the values dynamically in your code
Downside is your layout related work is spilled over to the code. I really don't like this.
Making child layout to extract common layout elements
Downside is your layout's hierarchy will be deeper and cluttered so it would be harder to figure out what's going on. Still, this is better than option #1 thanks to Hierarchy Viewer. I am not sure if this approach will be always achievable.
If you could share your tricks to get through this, it would be much appreciated.
I think I found a solution. I will accept it as an answer if others give thumbs up.
I found Configuration qualifiers described in Supporting Multiple Screens works not only for res/drawable and res/layout but also for res/values. So on my layout/some_layout.xml, I say like this:
<ImageButton
android:id="#+id/imagePlay"
android:layout_width="#dimen/button_size"
android:layout_height="#dimen/button_size"
android:scaleType="fitCenter"
android:src="#drawable/play" />
Then on values/layout.xml file you define the default button_size:
<resources>
<dimen name="button_size">44dp</dimen>
</resources>
And on values-xlarge/layout.xml file you define the xlarge mode button_size:
<resources>
<dimen name="button_size">66dp</dimen>
</resources>
I didn't try other values resources, but I assume it also works for Styles and Themes so in case your layout customization is a little bit more than a size or weight, you could define a style in the values and use it.
I am developing an application where certain elements will repeat themselves a lot over the whole interface. Googling a bit i found out about the <include /> tag, which is working nicely for what i want.
I was just wondering if there is a way to export certain properties of the included layout: One of them has an image and a string that change according to the use case, and i'd like to set these in the XML file for each case, instead of having to write boilerplate code to set them in the code; Is there any way to do this? Or am i doomed to write that code?
We're all doomed. As explained in the article Creating Reusable Components, the only things that you can override are the layout_* attributes and the id. There's no way (sadly) to parameterize a layout like you describe.
The <include> tag is is useful for separating configuration-dependent parts of your layout from those parts that are invariant across devices. (E.g., you can <include layout="#layout/footer"> and have different footer.xml files for different configurations.)