android – How to add documentation to custom attributes?-ThrowExceptions

Exception or error:

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

How to solve:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *