Whats the use of app namespace in android xml
Asked Answered
P

3

8

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>
Precede answered 16/3, 2016 at 18:44 Comment(0)
B
14

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>
Boyer answered 5/7, 2016 at 11:23 Comment(6)
Thank You ! So is app a standard keyword for namespace for custom attributes or can we use any word as namespace ?Precede
You can actually use any word, but you can save your brain from thinking by using the standard app one. Actually in Android Studio if you type 'app' then it will try to autocomplete the whole line with 'appNs' shortcut for you, so you can save your finger from typing too!Boyer
@ChetaGowda if you find my explanation helpful, would you mind accept my answer as your solution. :)Boyer
Done. Thanks for the help.Precede
Nice explanation!Conspicuous
@KenRatanachaiS. Sir how does android comes to know which custom attribute belongs to which external library?Honorary
D
2

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

Denisdenise answered 4/7, 2016 at 22:10 Comment(1)
The namespace does not have to be the app name. In fact, it is best practice to use the app namespace for attributes supported by the support library.Boyer
R
1

support.v7 is a compatibility older Android version. To use this, you need to add a Gradle dependencies:

dependencies {
        compile 'com.android.support:appcompat-v7:23.0.1'
}
Reactionary answered 16/3, 2016 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.