Difference between declare-styleable and style

Stephan picture Stephan · Jan 3, 2011 · Viewed 17.7k times · Source

I've begun playing around with styles and such in my android applications, and I have gotten everything working so far. I quite understood the 'style' section of the guide.

But, looking around, as in this thread, I can't really figure out the difference between the two (declare-stylable and style). From my understanding declare-styleable takes the attribute specified in it and specifies it as styleable, and then from the code one changes it as he wants.

But if this is what it really does, wouldn't it be simpler to just define the attribute in the layout? Or declare a style specifying it?

Answer

winitzki picture winitzki · Feb 3, 2012

I think there is only the following difference between declaring an attribute as styleable or not.

In attrs.xml you can declare custom attributes, either directly within the "resources" section or within "declare-styleable":

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <attr name="attrib1" format="string" />
 <declare-styleable name="blahblah">
    <attr name="attrib2" format="string" />
 </declare-styleable>

So now we defined "attrib1" as non-styleable and "attrib2" as styleable.

In layout/someactivity.xml we can use these attributes directly (no namespace needed):

<com.custom.ViewClass  attrib1="xyz" attrib2="abc"/>

You can use the "styleable" attribute "attrib2" within a style.xml declaration. Again, no namespace is needed here (even if a namespace was used in the layout XML).

 <style name="customstyle" parent="@android:style/Widget.TextView">
    <item name="attrib2">text value</item>
    <!--  customize other, standard attributes too: -->
    <item name="android:textColor">@color/white</item>
 </style>

Then you can also set the attributes per style.

<com.custom.CustomView attrib1="xyz" style="@style/customstyle"/>

Let us assume that we do this: we set attrib1 directly in XML, and we set attrib2 in a style.

Elsewhere I have seen descriptions stating that "blahblah" must be the name of the custom view class that uses these attributes, and that you need to use a namespace to refer to your custom attributes in the layout XML. But none of this seems to be necessary.

The difference between styleable and non-styleable seems to be that:

  • You can use styleable attributes in "style.xml" declarations.
  • The constructor of the custom class needs to read the styled and the non-styled attributes in a different way: the styled attributes with obtainStyledAttributes(), and the non-styled attributes with attr.getAttributeValue() or similar.

In most tutorials and examples I've seen on the Web, only the obtainStyledAttributes() was used. However, this does not work with attributes declared directly in layout, without using a style. If you do obtainStyledAttributes() as shown in most tutorials, you will not get the attribute attrib1 at all; you will only get attrib2 since it was declared in the style. The direct method using attr.getAttributeValue() works:

 public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    String attrib1 = attrs.getAttributeValue(null, "attrib1");
    // do something with this value
 }

Since we used no namespace to declare "attrib1", we pass null as the namespace argument in getAttributeValue().