I have a set of custom Android layout parameters defined in attrs.xml. Now I would like to use some tags in my styles.xml file.
At the moment I get this error:
error: Error: No resource found that matches the given name: attr 'custom:tag'
I have tried declaring custom XML namespace as follows:
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.my.project"
>
hoping, that the same logic used in every layout declaration can be applied here, but with no success.
The XML namespace mechanism is used to namespace tags and attributes. When you define a style like this:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.my.project">
<style name="my_style"> <item name="custom:tag">some_value</item> </style>
</resources>
you are trying to apply XML namespacing to an attribute value, which won't work. In this case, you should specify the package name directly, like this:
<style name="my_style"> <item name="com.my.project:tag">some_value</item> </style>
Now Android will be able to resolve where the attribute is defined.
The accepted solution did not work for me, but it shed some light upon the situation.
The custom attributes are resolved and can be referenced in a global project's package name, like "com.ltst.project". Even if you have multiple modules (with the same base package name) the resources would be resolved in a project's package name.
So for me it was enough to just omit any prefixes for custom attributes in a style.
Custom attribute:
<declare-styleable name="SampleView">
<attr name="sample_color" format="reference" />
</declare-styleable>
Style:
<style name="SampleStyle">
<item name="sample_color">#color/sample_color</item>
</style>
You can use the link
xmlns: app = "http://schemas.android.com/apk/res-auto
and define the prefix for each tag as app
Related
I'm not able to find a way to insert the action bar size attribute in my own xml file. Since I'm using Google maps in my activity, I can't use the built-in xml attribute at all in my layout, but I have to call setPadding() on the map using the action bar size. Now, I found the way to retrieve the actionBarSize at runtime, but I wonder if I could skip this code completly, inserting the android attribute in my onw xml, for example:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="myownsize">#android:attr/actionBarSize</dimen>
</resources>
But in this way it doesn't work. Is there a way to do it?
Define a attr in your own project. Create res/values/attrs.xml and add this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myownsize" format="dimension"></attr>
</resources>
In your res/values/styles.xml, under your parent theme definition, initialize this attribute:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="myownsize">?android:attr/actionBarSize</item>
</style>
Once this is done, you can use ?attr/myownsize as a dimension:
android:padding="?attr/myownsize"
Sorry if I too, misunderstood the question.
I want to define an android style that extends one defined in a different Application/package not imported as library.
From the definition of an xml reference to an android resource here:
#[<package_name>:]<resource_type>/<resource_name>
seems that is possible to specify an external where the resource is defined ( as used to reference system resource #android:string/name )
Now, i have my main app ( package: com.example.test ) that define an attribute and a theme like this:
<declare-styleable name="TestAttrs">
<attr name="testColor" format="color" />
</declare-styleable>
<style name="TestTheme" parent="#android:style/Theme">
<item name="testColor">#FFFF0000</item>
</style>
if in a second, different app I try to use the previous package to define a style using the syntax shown above I get an error saying that the resouce can't be found.
<style name="SubTheme" parent="#com.example.test:style/TestTheme">
<item name="com.example.test:testColor">#FF00FF00</item>
</style>
is it possible to tell the compiler where to look for the package?
do i have to define my initial style/attributes as shared?
Did you declare the namespace:
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.example.test="...external resource's namespace">
package_name does not refer to external applications on the device, but other library-project packages you are building against (each have their own resource table, which is then merged).
To access private resources in external applications you can use PackageManager.getResourcesForApplication() or expose a ContentProvider and do it yourself.
Here's a resource of mine:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme">
<item name="editTextStyle">#android:style/Widget.EditText</item>
</style>
</resources>
Eclipse doesn't like it: "No resource found that matches the given name: attr 'editTextStyle'"
At the same time, if I open Android's own platforms/android-3/data/res/values/themes.xml, I see that this is clearly being used there:
<resources>
...
<style name="Theme">
...
<item name="editTextStyle">#android:style/Widget.EditText</item>
...
Android documentation encourages to read that very same file to learn about the ins and outs of styling. But how can I actually do the same cool things they do there?
You need to specify the android namespace:
<item name="android:editTextStyle">#android:style/Widget.EditText</item>
Also, you probably only want to list the items you are overriding, there is really no point in setting it to the default.
I'm a using a view flow component into my project, which allows the dev to override some attributes, like that :
<org.taptwo.android.widget.TitleFlowIndicator
...
app:footerTriangleHeight="0dip"
... />
I am reusing this component into several layouts, and I would like to put the properties into a style.
But when I'm doing this, the parser says Error: No resource found that matches the given name: attr 'app:footerTriangleHeight', even if I add the namespace in the styles file.
Is there a way to do that in android ?
Thanks.
Android Application
If you are not using Android Libraries, then here is what you can do:
Define custom styleable attribute (I guess you've already done that).
Do not use your namespace prefix in style items (namespace defaults to current app's namespace).
Example:
In attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="testAttr" format="string"/>
</resources>
In styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TestStyle" >
<item name="testAttr">asdf</item>
</style>
</resources>
Android Library
If custom attribute comes from Android Library, you can still use described approach. It theoretically should work, because Android Library's namespace is the same as application's (from aapt tool perspective during the build). But I haven't test this myself.
If you're specifying namespace, it will show error. As far as I know, styles do not support xml namespaces. So this will fail:
In styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:app="http://schemas.android.com/apk/res/app.package.name">
<style name="TestStyle" >
<item name="app:testAttr">asdf</item>
</style>
</resources>
Parser automatically defaults to current app's AndroidManifest namespace, though you can not specify this namespace explicitly.
You can define the style in terms of your namespace, i've used it myself
i.e
<style name="whatever">
<item name="YourPackage.YourSubPackage:parametername">#drawable_or_anything/bla</item>
</style>
I have a few custom views in my Android project and I've added the relevant details to the attrs.xml file. And now I can implement my objects through XML. This works fine.
How do I style these elements? When I try to use my custom attributes in the styles.xml is get an error "No resource found that matches the given name:"
For using the custom views in normal xml developement I use xmlns:app="http://schemas.android.com/apk/res/bla.bla.bla". What is the correct for use in styles?
This is what my style looks like currently
<style name="Journey_DaySelect_Sunday">
<item name="app:onImage">#drawable/day_sunday_selected</item>
<item name="app:offImage">#drawable/day_sunday</item>
</style>
After more intensive searching on Google I gave up finding it answered elsewhere, and by chance tried using the absolute namespace of my generated R file it worked. May this solve all your problems.
USE THE NAMESPACE CONTAINING YOUR R FILE
<style name="Journey_DaySelect_Sunday" parent="Journey_DaySelect">
<item name="AppZappy.NIRailAndBus:onImage">#drawable/day_sunday_selected</item>
<item name="AppZappy.NIRailAndBus:offImage">#drawable/day_sunday</item>
</style>
For clarification, the item's name attribute should be the same as what is in the attrs.xml's declare-styleable name attribute + ":" + the attribute name.
For example:
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.chuck.norris">
<attr name="actionBarTextColor" format="color"/>
</declare-styleable>
</resources>
style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myNewStyle">
<item name="android:textColor">#FFFF0000</item>
<item name="com.chuck.norris:actionBarTextColor">#ffff0000</item>
</style>
</resources>
You can then apply this style to all activities by using a theme in your manifest.xml file. Anywhere that a custom view exists that wants to use the "actionBarTextColor" attribute, you can then use the Java code:
TypedArray typedArray = context.obtainStyledAttributes(attrSet, R.styleable.com_chuck_norris);
COLOR_ACTION_BAR_TEXT = typedArray.getColor(R.styleable.com_chuck_norris_actionBarTextColor, 0xff0000ff);
typedArray.recycle();
I'm not sure why you cannot just define your schema in your style.xml file as was asked above, but it seems to be a limitation of style.xml.
try this solution
<style name="Journey_DaySelect_Sunday">
<item name="onImage">#drawable/day_sunday_selected</item>
<item name="offImage">#drawable/day_sunday</item>
</style>
reference(Chinese)
if you guys think it useful,I will translate it.