17

I am able to document pretty much everything in in my Android projects and generate nice-looking API references for them.

The only exception to this are the XML files, and especially the attribute files that contain the styleable attributes.

For example, a portion of res/values/attrs.xml:

<resources>
    <declare-styleable name="TimelineView">
        <attr name="drawCollapsed" format="boolean" />
    </declare-styleable>
</resources>

I noticed that in the Android source, standard attributes documentation is generated for R.

My generated documentation, obviously, includes some generic text for my attribute type (boolean, in this case):

auto-documented property

Is there an official specification for this type of documentation or a way to document attributes originating in XML such that the description appears in the auto-generated JavaDoc?

MasterAM
  • 16,283
  • 6
  • 45
  • 66

1 Answers1

20

I am not sure that this is an official standard, but I stumbled across it when writing my question. I decided to post the question and answer it anyway, for the sake of other who might encounter this issue.

I was able to generate attribute documentation by adding an XML comment above the attribute, which seems quite apparent now that I have seen it.

I initially tried it without rebuilding my project, which lead to the original lack of documentation. Once I rebuilt the module and generated the JavaDoc, I got the desired result.

The steps to follow are:

  1. Place a comment above desired attribute(s).

    <resources>
        <declare-styleable name="TimelineView">
            <!-- Initially draw collapsed until adapters have been set. -->
            <attr name="drawCollapsed" format="boolean" />
        </declare-styleable>
    
    </resources>
    

  2. Rebuild the relevant module/project.

  3. Generate the JavaDoc. Using Android Studio (currently 0.5.8).
    There is currently a small issue with the automatic generation, I am using the workaround introduced in the first linked post.
    The generated documentation should contain your comments.
    desired documentation

If anyone knows of any official sources for this or an official method, please don't hesitate to share them.

Update:
It seems this is indeed the way it is done in the Android source files, including some JavaDoc directives, HTML and child annotation in the comments, for example:

<!-- Alignment constants. -->
<attr name="alignmentMode">
    <!-- Align the bounds of the children.
    See {@link android.widget.GridLayout#ALIGN_BOUNDS}. -->
    <enum name="alignBounds" value="0" />
    <!-- Align the margins of the children.
    See {@link android.widget.GridLayout#ALIGN_MARGINS}. -->
    <enum name="alignMargins" value="1" />
</attr>
MasterAM
  • 16,283
  • 6
  • 45
  • 66
  • 1
    Are your comments on the XML resources reaching the output of the javadoc or R.java? I am creating the javadoc via gradle as I describe in a similar question, but the default documentation is added for my XML resources: http://stackoverflow.com/questions/35430792/adding-documentation-for-generated-r-java-files-in-android-studio – Petrakeas Feb 16 '16 at 11:15
  • @Petrakeas, I haven't done Android development recently, so I opened my original project and updated it to work with the latest version of AndroidStudio (v2.0-beta5). It seems that the data from the XML does not reach `R.java` and hence does not find its way into the JavaDoc, so it does not work as it used to. I am getting lots of `@attr is an unknown tag` warnings in the process, but I don't have time to dive any deeper into it. I did not try to generate the JavaDoc from gradle and I haven't tried DocLava, but the issue may related to [this](http://stackoverflow.com/q/33246400/268093). – MasterAM Feb 18 '16 at 12:19
  • 2
    I looked at e.g. [TextView](https://developer.android.com/reference/android/widget/TextView.html) and in the source (at the class doc) there is an entry for each XML attribute:`@attr ref android.R.styleable#TextView_text`. I guess this is how it gets to the class' javadoc, in the section "XML attributes". However, when I add the corresponding `@attr ref mypackage.R.styleable#MyClass_myattr` (and XML comments at myatrr) but the generated documenttion just contains the standard text. Further there are warnings on the console: "warning - @attr is an unknown tag." but actually for Android classes! – user905686 Oct 21 '16 at 19:35