177

Here is an example of the app namespace that I've seen from a res/menu/main.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never" />
</menu>

What purpose does the app namespace serve? Is it a "standard" Android XML namespace? Are the same value options available for the same attribute placed in two different namespaces (e.g. app:showAsAction and android:showAsAction).

From the docs: android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

i.e., would the line in the above example mean something else if the attribute were instead:

android:showAsAction="never"

It almost looks like it might be some sort of "subclassing" mechanism, but I can't seem to find any real documentation on the app namespace from Google/Android sources.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
coder123
  • 9,267
  • 5
  • 16
  • 13

2 Answers2

231

The app namespace is not specific to a library, but it is used for all attributes defined in your app, whether by your code or by libraries you import, effectively making a single global namespace for custom attributes - i.e., attributes not defined by the android system.

In this case, the appcompat-v7 library uses custom attributes mirroring the android: namespace ones to support prior versions of android (for example: android:showAsAction was only added in API11, but app:showAsAction (being provided as part of your application) works on all API levels your app does) - obviously using the android:showAsAction wouldn't work on API levels where that attribute is not defined.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Thank you! I'm happy to have finally found a mention of this in the documentation. One follow-up question, though. The action bar docs in your link says: "Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library." – coder123 Nov 01 '14 at 20:19
  • 1
    So what does happen on older devices where the attributes don't exist in the framework? It's not yet clear to me how defining a custom namespace works around missing support for an attribute. Does declaring `showAsAction` under a custom namespace mean that it works as expected on the newer platforms and is ignored on older ones? – coder123 Nov 01 '14 at 20:22
  • 4
    Attributes that don't exist are silently ignored. When you create a custom attribute, you are guaranteeing that the custom attribute will exist at runtime (obviously: its definition is included with your app). Therefore the support library uses custom attributes so that their custom code for building menus can use a single code path that works on all API versions, essentially replacing any need to use the `android:` versions. – ianhanniballake Nov 01 '14 at 20:41
  • 2
    I'm sorry if I'm not just not getting it. Can you help me understand how simply changing the namespace provides a definition for an attribute? If the `showAsName` attribute isn't supported in an older library, how does using a custom namespace allow the the platform to know the range of possible value options (`ifRoom`, `never`, etc.), and know how to interpret those options? I'm assuming that "attributes that don't exist" means attributes for which there exists no implementation in the library. Swapping `android:` for `app:` hardly seems like a complete workaround. Am I missing something? – coder123 Nov 01 '14 at 20:59
  • 16
    There's two parts to it: 1) the custom attribute (`app:showAsName`) and all of its possible values *are included in your app* when you include the `appcompat-v7` library (which defines it in its `attrs.xml` file). 2) The AppCompat library (specifically, [ActionMenuView](https://developer.android.com/reference/android/support/v7/widget/ActionMenuView.html) which gets used automatically when you use `ActionBarActivity`) parses and uses the `app:showAsAction` to properly show items in the same way on all API levels. It certainly does require both XML and code to work together. – ianhanniballake Nov 01 '14 at 21:05
  • I was trying to find more info about the topic in the "official" documentation but to no avail. Can you share a link to where you found the info in your answer. It seems like this namespace is not documented (as many other things lately). It is so frustrating.... – Ognyan Jul 28 '18 at 12:41
  • Is there any official documentation on this? – Just The Highlights Mar 09 '19 at 11:53
  • Yes, it's really difficult finding any relevant documentation on this as a beginner. I've been scouring youtube, google, and stackoverflow without much avail! – David Jan 26 '20 at 00:45
  • Thank you for your answer. These `app:`-prefixed attributes are declared without any prefixes inside `styles.xml`, right (e.g. `fontFamily`)? So when there are two choices to declare attributes, with the `android:` prefix or without the said prefix, is it correct to say that we can always choose the one without the prefix and it will still work on all API versions? – Richard Apr 11 '20 at 14:42
-1

You can get some explaination from this link

XML namespace

Namespace declaration An XML namespace is declared using the reserved XML attribute xmlns or xmlns:prefix, the value of which must be a valid namespace name.

For example, the following declaration maps the "xhtml:" prefix to the XHTML namespace:

xmlns:xhtml="http://www.w3.org/1999/xhtml"

Any element or attribute whose name starts with the prefix "xhtml:" is considered to be in the XHTML namespace, if it or an ancestor has the above namespace declaration.

It is also possible to declare a default namespace. For example:

xmlns="http://www.w3.org/1999/xhtml"

In this case, any element without a namespace prefix is considered to be in the XHTML namespace, if it or an ancestor has the above default namespace declaration.

If there is no default namespace declaration in scope, the namespace name has no value.[6] In that case, an element without an explicit namespace prefix is considered not to be in any namespace.

Attributes are never subject to the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace.

KIRPAL SINGH
  • 185
  • 2
  • 17