LambergaR
LambergaR

Reputation: 2473

Custom attrs parameter used in styles.xml

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.

Upvotes: 7

Views: 5247

Answers (3)

Kirill Starostin
Kirill Starostin

Reputation: 918

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>

Upvotes: 2

C&#237;cero Moura
C&#237;cero Moura

Reputation: 2333

You can use the link

xmlns: app = "http://schemas.android.com/apk/res-auto

and define the prefix for each tag as app

Upvotes: 0

Martin Nordholts
Martin Nordholts

Reputation: 10358

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.

Upvotes: 15

Related Questions