What is the 'app' Android XML namespace?

AndroidXmlAndroid LayoutXml NamespacesAndroid Tools-Namespace

Android Problem Overview


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][1]: 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.

[1]: http://developer.android.com/guide/topics/resources/menu-resource.html "docs"

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncoder123View Question on Stackoverflow
Solution 1 - AndroidianhanniballakeView Answer on Stackoverflow
Solution 2 - AndroidKIRPAL SINGHView Answer on Stackoverflow