0

I am sure everyone who has work with android before is familiar with the xml namespace declaration xmlns:android = xmlns:android="http://schemas.android.com/apk/res/android. Here is where my question lies.

If the namespace declaration above is just primarily for unique identification then where exactly did all the attributes like android:layout_width or android:layout_height come from?

The only way I can make sense of this is if xmlns:android="http://schemas.android.com/apk/res/android is a schema which contains predefined attributes as a result, you are allowed to modify all these attributes. If that is the case then where is this schema stored?

I have taken a look at the link above to see if my post is potentially a duplicate post but it is not. The other post just basically tells you what xmlns:android="http://schemas.android.com/apk/res/android does. However, I already know what the function of that name space declaration is. My concern is how does android knows what attributes are there for each elements when the namespace above reference nothing? or at least I think that the namespace above is referenced to nothing.

Wowzer
  • 1,103
  • 2
  • 12
  • 26
  • Possible duplicate of [Regarding Android xmlns](http://stackoverflow.com/questions/19020171/regarding-android-xmlns) – OneCricketeer Dec 12 '16 at 22:55
  • Regarding your edit, [there have been several similar questions](http://stackoverflow.com/search?q=%22xmlns%3Aandroid%22). And there has been no clear answer, at least that I have found. What you are asking is very clearly documented here. https://developer.android.com/guide/topics/resources/layout-resource.html – OneCricketeer Dec 12 '16 at 23:13
  • And as those answers say "its a URI, not a URL", which means, you can't just go to that address in the browser, and expect a result to come back – OneCricketeer Dec 12 '16 at 23:16
  • I am so sorry if I confused you. Maybe my question is not very clear so let me re-clarify it for you. Whenever you type things like android: inside an element tag like relative layout, android gives you suggestion on what that attributes could be. Things like android:background, android:layout_width, android:padding. Now my question is not what these attributes are and what they do but where are these attributes defined. I am sure they have to be somewhere so that android knows to pull them up for the specific element. – Wowzer Dec 12 '16 at 23:17

1 Answers1

1

The namespace doesn't reference nothing. It's necessary to know which attributes to pull.

Here is a snippet of the Android Source Code for a ViewGroup.

<declare-styleable name="ViewGroup_Layout">
    <!-- Specifies the basic width of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant width or one of
         the special constants. -->
    <attr name="layout_width" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
    <!-- Specifies the basic height of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant height or one of
         the special constants. -->
    <attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
</declare-styleable>

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/attrs.xml#3005

how does android knows what attributes are there for each elements

No different than defining custom attributes for your custom View class, it pulls from a res/values/attrs.xml that is located within the SDK.

Referencing a section there.

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]

So, android is just the namespace for the built-in attributes.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Gee thanks this is precisely what I was looking for. But just a quick part that I don't understand. res/values/atttrs.xml doesn't seem to fit anywhere into "http://schemas.android.com/apk/res/android". It is kinda weird to me due to the fact that res/values != res/android and yet you can still reference the attributes. Do you mind elaborating on this part a little? – Wowzer Dec 12 '16 at 23:28
  • There is `your.package.name.R` for accessing your resources from Java, then there's `android.R` for accessing SDK resources. It's the same concept, just that's how the XML works. `res/[your package name]` == `your.package.name.R` and `res/android` == `android.R` – OneCricketeer Dec 12 '16 at 23:32
  • Oh ok so res/android == android.R but if that is the case shouldn't I need to do res/android/attr to reference the attributes in the declare styleable? – Wowzer Dec 12 '16 at 23:37
  • Nope. Go ahead and try it. Probably won't work. That namespace is for all resources within the SDK attrs.xml file, which gets bundled in along with all other resources and is available within `android.R`. The /attr would just be redundant, in my opinion – OneCricketeer Dec 13 '16 at 00:32