21

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

cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171

2 Answers2

6

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}
Ralph Bergmann
  • 3,015
  • 4
  • 30
  • 61
-1

You need to write constructors in code and then link them to the xml. Read this for more info.

Community
  • 1
  • 1
Steven Mann
  • 558
  • 3
  • 14