What is the 'app' Android XML namespace?
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.
What is XML property xmlns:app?
Xmlns stands for 'XML Namespace'
- The part after ':' is the prefix for the Namespace
- The part after '=' is the Namespace URI (the correct name for his part is actually "Namespace name").
(For further details see https://en.wikipedia.org/wiki/XML_namespace)
The namespace 'schemas.android.com/tools' is for specifying options to build the app by Android Studio, and are not included in the final app package
The namespace 'schemas.android.com/apk/res-auto' is used for all custom attributes - defined in libraries or in code. See this answer for details.
Note that any prefix can be used for a namespace, it is not mandatory to use 'app' for schemas.android.com/apk/res-auto. But the same prefix must be used when defining the custom attributes in the document, otherwise an error will be shown.
So, because met_maxCharacters is a custom attribute, it is shown when the 'schemas.android.com/apk/res-auto' namespace is used, and not with
'schemas.android.com/tools'
Whats the use of app namespace in android xml
There're two versions of ShareActionProvider, the android framework's and the v7 support library's.
In Sunshine you need to support min SDK version 10, while ShareActionProvider was added into the framework from API level 14, so how to provide the feature to SDK 10-13? You use the support library version instead.
You import the support library in build.gradle here
dependencies {
...
compile 'com.android.support:appcompat-v7:21.0.2'
}
Now back to your question about the app namespace. Sunshine is using the attributes that are not available in the framework on the lower SDKs they support (actionProviderClass and showAsAction), so they need to use the custom attributes provided by the support library, and to use the custom attributes you need to use the app namespace. The android namespace is for the framework attributes as the name suggested.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_share"
android:title="@string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Namespaces: how to know how and when to use them? i.e.: xmlns:andorid, xmlns:app, xmlns:tools
For android
& app
namespaces, use this link.
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 theandroid:
namespace ones to support prior versions of
android (for example:android:showAsAction
was only added in API11,
butapp: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.
About tools
;
Android Studio supports a variety of XML attributes in the tools
namespace that enable design-time features (such as which layout to
show in a fragment) or compile-time behaviors (such as which shrinking
mode to apply to your XML resources). When you build your app, the
build tools remove these attributes so there is no effect on your APK
size or runtime behavior.
There are also another usages of tools
namespace which you can see in the strings or etc like:
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
Which helps to ignore errors.
Also, there is another which helps to define TargetedAPI
:
tools:targetApi
And so much more.
Read: https://developer.android.com/studio/write/tool-attributes
Difference between android: and app: prefix in Android XML?
android
is usually used for attribute coming from Android SDK itself.
app
is often used if you are using the support library.
You may also see other namespaces if you are using custom views (of your own or form a library).
Here is some extra information: http://developer.android.com/training/custom-views/create-view.html#customattr
Why do we specify namespace in android xml file?
From developer.android.com
xmlns:android
Defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".
xmlns:android is for identification that this xml is used for android, not for other function.
Namespaces uniquely identify code/libraries. If I write an api that uses all the same names and such as the android api the only way to distinguish between my api and android api is to use the android namespace, or mine.
You can further refer http://en.wikipedia.org/wiki/XML_namespace
Related Topics
How to Set a Reminder in Android
Broadcastreceiver Not Receiving Boot_Completed
Android 4.3 Menu Item Showasaction="Always" Ignored
Embed Zxing Library Without Using Barcode Scanner App
In Eclipse, Unable to Reference an Android Library Project in Another Android Project
Change the Circle Color of Radio Button
How to Build an APK File in Eclipse
How to Add a Custom Button State
Foreground Service Being Killed by Android
How to Use Searchview in Toolbar Android
What Is the 'App' Android Xml Namespace
Set Selected Item of Spinner Programmatically
Android Emulator Doesn't Take Keyboard Input - Sdk Tools Rev 20
Android Asynctask Example and Explanation
Recyclerview Horizontal Scroll Snap in Center
How to Display Inline Images from HTML in an Android Textview