0

In the following snippet,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

</LinearLayout>

what is the class attribute, if it is the our Fragment class, then why is FragmentLayout$ appended to its name, and why is it without android or any namespace?

I can totally see that it is the name of the Fragment class, because TitlesFragment is the Fragment class in the example I took this code from, and com.example.android.apis.app tells me that we have to give this attribute a fully qualified name of the class, but what is FragmentLayout$ part appended to the name of the class, that is TitlesFragment?

I have taken this code from the example in this developer guide, but there is no explanation of the class attribute. I could not find anything relevant here in the reference either. Pardon me if it is something too fundamental.

Solace
  • 8,612
  • 22
  • 95
  • 183

1 Answers1

1

It does refer to the Fragment class. FragmentLayout$TitlesFragment means that TitlesFragment is a public, static nested class in the FragmentLayout class. If you inspect that class, you'll see that it's a ListFragment subclass.

public static class TitlesFragment extends ListFragment {

The only reference to that notation I've ever seen in the developer pages is on the Custom Components page. (It's under sub-heading 4. Use the Custom Component.)

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thank you for the answer. I just inspected a bit and learned [here](http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name) that $ is used "internally" by the compiler to decorate certain variable names, e.g. when an `InnerClass` class inside an `OuterClass` class is compiled, the `.class` formed will be named `OuterClass$InnerClass.class` by the compiler. This confuses me, is this named by the compiler, or do we just simply use a `$` sign to refer to the `.class` version of an innerclass, which we normally refer with the dot notation: OuterClass.InnerClass? – Solace Dec 16 '14 at 04:49
  • Secondly, isn't the `android:name` attribute already there to refer to the fragment class? – Solace Dec 16 '14 at 04:51
  • 1
    Thank you for the link, it pretty much gives an idea to quite an extent. – Solace Dec 16 '14 at 04:55
  • 1
    That post wouldn't be relevant here, since it pertains to the Java language. We're taking about Android layout xml; the Java compiler never touches that directly. As for the `android:name` attribute, as far as I know, it is almost the same thing, in that if `class` isn't found, `name` is then used. However, I believe that you must use `class` in this particular instance; i.e., with that notation for a static inner class. – Mike M. Dec 16 '14 at 05:03
  • 1
    [This post](http://stackoverflow.com/questions/10162983/activity-layout-fragment-class-vs-androidname-attributes) discusses `class` vs `name`. – Mike M. Dec 16 '14 at 05:20
  • 1
    I was incorrect. I just ran a test, and you can use the `android:name` attribute with the `$` notation. So it would seem that `class` and `android:name` are completely interchangeable. As a comment in the link I posted suggests, `class` just might be a hold-over from the early days of Fragment development. – Mike M. Dec 16 '14 at 05:27