Difference between android namespace - android

What is the difference between given two namespace decalration in Android.
xmlns:app="http://schemas.android.com/tools"
and
xmlns:app="http://schemas.android.com/apk/res-auto"
I am getting lint warning on using http://schemas.android.com/tools => Suspicious namespace and prefix combination
I know using http://schemas.android.com/apk/res-auto will solve the issue, but want to know the reason behind it.

Below namespace is used for android predifined components.
xmlns:app="http://schemas.android.com/tools"
But if you want to use custom components like appCompat or your custom defined views you have to use the other namespace as well
xmlns:app="http://schemas.android.com/apk/res-auto"

Related

What is the meaning of such lines like xmlns:app="http://schemas.android.com/apk/res-auto" in android xml file?

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"
When you want to import attributes in AndroidManifest.xml you have to kind of import their libraries. which you can then access by their prefixs {android:,tools:,app:}
Example of of usage are
android:name="yourpackege.App"
tools:overrideLibrary="yourpackege.App"
app:showAsAction="never"
In the above examples attributes being
android:name, tools:overrideLibrary, app:showAsAction
Of course every single prefix has many more attributes for example
android:name,android:theme,android:value
Kindly note though that
The app namespace is not specific to a library, but it is used for all attributes defined in your app readhere
Just to test the explanation above if you remove lets say
xmlns:android="http://schemas.android.com/apk/res/android" and rebuild your android application, you will get and error that looks something like

Namespace prefix “app” cannot find declaration

While I try to add wenchaojiang AndroidSwipeableCardStack library to my project. While I succeed to add this library but got an error in the following case.
In main Activity layout, I required adding the,
<com.wenchao.cardstack.CardStack
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:padding="10dp"
/>
Also I need to add the following with this
app:card_enable_loop="false"
app:card_enable_rotation="true"
app:card_gravity="top"
app:card_margin="20dp"
app:card_stack_size="4"
I got error as Decleration not found or error: attribute 'business.contacts.cardwithlib:card_enable_loop' not found.
I try many solutions like,
Include card 'com.android.support:cardview-v7:27.1.1' , add auto-generating xmlns:app="http://schemas.android.com/apk/res-auto" and Open their project and work fine for me in that project
I noticed that you are no including the Android's CardView but what it looks like a 3rd party library.
com.wenchao.cardstack.CardStack
Is this intentional? If so, maybe this component doesn't extend the CardView element, those specific style attributes might not be available.

app:something...What is app:?

I am using this famous ViewPagerIndicator library in my project. I wanted to add color on ViewPageIndicator. I found a solution from here on StackOverflow.
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
app:fillColor="#FF888888"
app:pageColor="#88FF0000"
app:strokeColor="#FF000000" />
But one thing I can't understand here: why it is written app: instead of android:? What does app: mean? I have never used app:? What is its namespace? I googled but no solution found.
For the namespace, add this: xmlns:app="http://schemas.android.com/apk/res-auto"
The app namespace is used for all 'new' attributes. For example: if I make a custom view, that has its own attributes (let's say RainbowView with new attribute 'numberOfColors'), then I have to use that attribute in the app namespace because it isn't declared in the android namespace. So use the app namespace for all custom attributes. Either attrs you defined yourself, or attrs the author of a library added (in your case for CirclePageIndicator). But most of the time, when you start typing 'strokeCo...' and you see 'strokeColor' in the autocomplete list, the namespace will be added automatically when you select it.
Probably you only need to add the missing namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
Or take a look at this answer for referencing a library:
How can I set up custom namespaces in layout file in Android studio?

difference in naming xml between res-auto and com.package.name - android

I have seen custom xml with :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
and
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/com.package.custom"
whats the difference between these two separate names?
Is the latter only points to default location like your package?
Is the former points to the reference lib ?
thanks.
If we add a new custom view and its attributes inside our project, you add this at the beginning of your layout:
xmlns:custom="http://schemas.android.com/apk/res/your_main_app_package
If the new custom view is inside a library project linked to your project, you add this:
xmlns:custom="http://schemas.android.com/apk/res-auto
Note: This problem has been fixed in ADT revision 17+ . For any services or Activities, declare the namespace as follows:
xmlns:custom="http://schemas.android.com/apk/res-auto"
The suffix res-auto will be replaced at build time with the actual project package, so make sure you set up your attribute names to avoid collisions if at all possible.

What is the meaning of xmlns:tools in Android XML layout?

For example, in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
Do I need to put it?
It defines the XML namespace of the document. You should put it, otherwise tags like <RelativeLayout> could be not recognied by the parser.
Namespaces are a way for XML documents to include tags from various vendors. By using xmlns attribute you declare, that, by default, you're using XML elements defined here: http://schemas.android.com/apk/res/android (note that this link is broken - this discussion explains why).
You also declare additional namespace, tools, which is not your default namespace, thus when referencing elements or attributes defined there, you must add tools prefix, on example:
tools:context=".SomeActivity"
Following is a useful link from Android dev portal: https://developer.android.com/studio/write/tool-attributes.html
It says
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.
i.e. tools namespace helps designing UI and all attributes with prefix 'tools' will be removed at build time.
In fact, when you do :
<RelativeLayout android:id> </RelativeLayout>
Instead of calling android:id, the xml will call http://schemas.android.com/apk/res/android:id . It just the page that declare all the attribute and views that you can use in your xml.
Here is an explanation.
http://www.w3schools.com/xml/xml_namespaces.asp

Categories

Resources