Android - get styleable value by name - android

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'.

Related

Get resource ID (R.string.name) of string value from Android attr in custom view

My application has a custom view, styled by attrs.xml entry:
<resources>
<declare-styleable name="MyView">
<attr name="title" format="reference" />
<attr name="image" format="reference" />
</declare-styleable>
</resources>
In my custom view, I need to retreive the resource ID of resource specified as title in attrs.xml, but I can only retreive the content of string specified as title. Fo far, I've tried using:
attrs.getAttributeResourceValue(R.styleable.MyView_title, 0)
And
getSourceResourceId(R.styleable.MyView_title, 0)
But in both cases, the returned value is the default 0.
I've tries the same approach with other types of attributes, and for example, Drawable ID can be easilly retrieved by:
getResourceId(R.styleable.MyView_image, 0)
Is there any way to check what R.string was provided in XML?

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

Define a required attribute for <declare-stylable>

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?

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"

How to add documentation to custom attributes?

I know how to create custom attributes for views, for example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SomeViewsCustomAttrs">
<attr name="someAttr" format="dimension" />
<attr name="anotherAttr" format="boolean" />
</declare-styleable>
</resources>
I am wondering if there is a way to add documentation to these custom attrs. While editing layouts in the XML editor, you can get tooltips that describe what the attrs are. As an example, if you are typing android:layout_width= it will give you the following information, "Specifies the basic width of the view. [dimension, enum]"
Is there a way to provide that for your own attrs?
Thanks
Add XML comment to every element:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SomeViewsCustomAttrs">
<!-- Default highlight color for items that are pressed. -->
<attr name="colorPressedHighlight" format="color" />
<!-- Default highlight color for items that are long-pressed. -->
<attr name="colorLongPressedHighlight" format="color" />
</declare-styleable>
</resources>
And in the Java Doc in the Java source file of your view link to the attributes like this (example from TextView):
* See {#link android.R.styleable#TextView TextView Attributes}, {#link android.R.styleable#View View Attributes}
You need to write constructors in code and then link them to the xml. Read this for more info.

Categories

Resources