Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the use of app namespace in android xml

Below is the code of a menu being displayed in an activity(DetailFragment.xml of Sunshine udacity android course)

I could not understand why two different namespaces are needed below. Why cant I use the namespace android: instead of app:

In below xml part when I replace app:actionProviderClass="android.support.v7.widget.ShareActionProvider" with app:actionProviderClass="android.widget.ShareActionProvider"

It seemed to give some assignment error, but works fine if app is changed to android as below android:actionProviderClass="android.widget.ShareActionProvider"

I am not able to understand what exactly is happening here.

    <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>
like image 986
Chetan Gowda Avatar asked Mar 16 '16 18:03

Chetan Gowda


People also ask

What is the use of namespace in XML?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

What is namespace in android XML?

An XML Namespace has the properties: Namespace URI: Namespace name expressed as a URI to which the prefix is bound. prefix: syntactically, this is the part of the attribute name following the XMLConstants. XMLNS_ATTRIBUTE ("xmlns") in the Namespace declaration.

What is an app 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.

What is the difference between app and android in 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). Thanks.


2 Answers

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>
like image 61
Ken Ratanachai S. Avatar answered Sep 20 '22 15:09

Ken Ratanachai S.


I'm currently also going through Udacity course as you are. I was having problem with the specifying the actionProviderClass in the detailFragment.xml. Turns out the actionProviderClass attribute must use the namespace of the app name. I think this might be solution you're looking for.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android = "http://schemas.android.com/apk/res/android"
      xmlns:Sunshine = "http://schemas.android.com/apk/res-auto">
    <item
        android:id = "@+id/action_share"
        android:title = "@string/action_share"
        Sunshine:actionProviderClass = "android.support.v7.widget.ShareActionProvider"
        Sunshine:showAsAction = "always"/>
</menu>

Notice the actionProviderClass attribute is using the namespace of the app name.

Solution found from: link .The first answer

Also make sure the build.gradle for the app is the same as link

like image 43
Clumsy-Coder Avatar answered Sep 18 '22 15:09

Clumsy-Coder