Define a required attribute for <declare-stylable> - android

Is it possible to define the stylable attributes as required? (as in XSD schemas)
<declare-styleable name="PieChart">
<attr name="name" format="string" use="required"/>
</declare-styleable>
The above XML does not work, is there another syntax to achieve it?

What if you throw exception in your constructor in case the attr value is null?

Related

Android design support and other library give gradle build error: "headerLayout" has already been defined

I would like to add the design suppport library (com.android.support:design:22.2.0) but I got this error message:
"headerLayout" has already been defined
I tried to find which library use this from my library list and maybe this one:
https://github.com/traex/ExpandableLayout
How can I use theme same time? Thank you!
Edit:
here is some detail:
Error:(1) Attribute "headerLayout" has already been defined ...
...debug\values\values.xml:440: error: Attribute "headerLayout" has already been defined
And this is the 440. line:
<declare-styleable name="NavigationView">
<attr name="android:background"/><attr name="android:fitsSystemWindows"/>
<attr name="android:maxWidth"/><attr name="elevation"/>
<attr format="reference" name="menu"/>
<attr format="color" name="itemIconTint"/>
<attr format="color" name="itemTextColor"/>
<attr format="reference" name="itemBackground"/>
<attr format="reference" name="headerLayout"/>
</declare-styleable>
and I found another item with this reference name, which is come from ExpandableLayout:
<declare-styleable name="ExpandableLayout">
<attr format="reference" name="headerLayout"/>
<attr format="reference" name="contentLayout"/>
<attr format="integer" name="duration"/>
</declare-styleable>
What is the solution? Because I can't modify these properties.
It would be more useful if you post your Error log and a code snippet for the affected code.
It does not seem like a dependency error, check to see if you have a variable called headerLayout and see you have that declared more than once.
Edit:
A workaround would be to change one of the name "headerLayout" to something else. Of course all reference to it must also be updated. This is an ambiguity error.
This is an old question but i will answer anyway, maybe will help someone.
Change de version of ExpandableLayout to:
compile 'com.github.traex.expandablelayout:library:1.3'
Than you will have other issues:
Error:(16) No resource identifier found for attribute 'contentLayout' in package ....
change:
contentLayout ---> el_contentLayout
headerLayout ---> el_headerLayout
if you call the contentLayout or header layout in JAVA, change:
getHeaderRelativeLayout() ----> getHeaderLayout()
getContentRelativeLayout() ----> getContentLayout()
and everything will work just fine :)

Custom Attribute Error - Android Studio 1.2

In my Android project I have a couple of custom components that use custom attributes.
The attrs.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name = "TextBox">
<attr name = "font" format = "string"/>
</declare-styleable>
<declare-styleable name = "ButtonBox">
<attr name = "font" format = "string"/>
</declare-styleable>
</resources>
I am pulling in the attributes just fine in the custom component, but when I go to run the code I see the following error.
Error: Found item Attr/font more than one time
Error: Execution failed for task ':app:mergeDebugResources'.
It shouldn't make a difference that there are similar attribute names in two different declare-styleable resources correct?
If you have any help it would be greatly appreciated, thank you!
As you can see here, the attr itself can have multiple properties and can be defined only once and you can configure multiple details inside it.
So you should give it different names or, since they have the same properties, use only one declare-styable for both.
Check out this link too, there's a good example.
Here is how it should be:
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name="Box">
<attr name="font" format="string"/>
</declare-styleable>
</resources>
You can use Box on text, button, etc.
Android — Custom Views: same Attribute on multiple Custom Views
You may have tried something like the code below in your attrs.xml:
'''
<resources>
<declare-styleable name="MyComponent1">
<attr name="myAttr" format="string" />
</declare-styleable>
<declare-styleable name="MyComponent2">
<attr name="myAttr" format="string" />
</declare-styleable>
</resources>
'''
But this does not work, and we get an Error:
Found item Attr/myAttr more than one time.
How do native views handle that?
Native Views have the same attribute names and don’t suffer from this problem.
Let’s sneak into their code and see how these attributes are declared.
Looking into their code, we can see that they create the attr tag outside declare-styleable, and then, inside of it you just need to reference it only by its name, there is no need to declare format again.
Our code should be just like that:
<resources>
<attr name="myAttr" format="string" />
<declare-styleable name="MyComponent1">
<attr name="myAttr" />
</declare-styleable>
<declare-styleable name="MyComponent2">
<attr name="myAttr" />
</declare-styleable>
</resources>
can also read this fantastic medium paper :
same Attribute on multiple Custom Views

Can't use globally defined custom attribute

So I have defined a custom attribute:
<resources>
<attr name="filterColor" format="color"/>
</resources>
I did not declare it in an to reuse it in other custom views.
However, I am not able to use it like so
<SomeView
app:filterColor="#color/my_color"/>
Nor am I able to obtain it from AttributeSet
final TypedArray a = getContext().obtainStyledAttributes(attrs, new int[]{R.attr.filterColor});
It's driving me crazy - why are we allowed to declare global custom Attributes then if we can't use them?
If nothing else, your XML appears to be missing the <declare-styleable> element:
<resources>
<declare-styleable name="ColorMixer">
<attr name="color" format="color" />
</declare-styleable>
</resources>
(pulled from this library project)
The name in declare-styleable should be the class name of the view that will use the attribute (SomeView in your case).
This is covered in the documentation.

Android - get styleable value by name

Is there a way to get styleable by name, for example if I have string "Text" is it possible to get R.styleable.CustomView_Text value (by value I mean just index in R.styleable.CustomView array, not attribute value) without reflection?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="Text" format="string" />
</declare-styleable>
</resources>
Use the Resources.getIdentifier method, setting the defType param as 'styleable'.

Custom ListView - How to set attrs.xml as a namespace

I follow this guide to make a custom ListView for my app. The tutorial uses a namespace called ottasee, which should be defined as a xml-namespace inside the element. So here is some of my code:
<com.my.app.Layout.CustomListView
xmlns:ottasee="what_should_i_put_here?"
android:id="#+id/lastCases"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#null"
android:scrollbars="none"
ottasee:dropShadowBottom="true"
ottasee:dropshadowrightsrc="#drawable/drop_shadow"
/>
I can see that the attributes ottasee:dropShadowBottom and ottasee:dropshadowrightsrc are part of my attrs.xml in the values folder. Like this:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="DropShadowListView">
<attr format="boolean" name="dropShadowLeft"></attr>
<attr format="reference" name="dropShadowLeftSrc"></attr>
<attr format="boolean" name="dropShadowRight"></attr>
<attr format="reference" name="dropShadowRightSrc"></attr>
<attr format="boolean" name="dropShadowTop"></attr>
<attr format="reference" name="dropShadowTopSrc"></attr>
<attr format="boolean" name="dropShadowBottom"></attr>
<attr format="reference" name="dropShadowBottomSrc"></attr>
</declare-styleable>
</resources>
How should I define the xml namespace for the ListView in order to grab the attributes from the attrs.xml file?
Thanks!
EDIT
My CustomListView is under the package com.my.app.Layout and I tried to declare the ns this way: xmlns:ottasee="http://schemas.android.com/apk/res/com.my.app.Layout
But I only get an error in my xml file:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'dropShadowBottom' in package
'com.my.app.Layout'
- error: No resource identifier found for attribute 'dropshadowrightsrc' in package
'com.my.app.Layout'
How can I accomplish setting the right ns?
You should add the following the following to include the necessary attributes to your namespace:
xmlns:ottasee ="http://schemas.android.com/apk/res-auto"
Slightly different to #budius's answer as it will auto-resolve the package name for you.
usually the XML editor do it for you, I've even never worried, but it's like that:
xmlns:ottasee="http://schemas.android.com/apk/res/com.your.package.name"

Categories

Resources