What is the meaning of xmlns:android? - android

Why is everyone use http://schemas.android.com/apk/res/android ?
Do I need sometimes to use something different that this url ?
Who need this url ?, is something like package I mean what is the point with it
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"

xmlns defines an XML Namespace.
The URI that's used, even if it doesn't point to an existing resource, generaly points to (quoting) a resource under the control of the author or organisation defining the vocabulary.
For more informations, take a look at XML namespace.

Why is everyone use http://schemas.android.com/apk/res/android ?
Because that describes the namespace. It means that you can tell the difference between the orientation attribute used there, and an orientation attribute used in some other XML language.
Do I need sometimes to use something different
Yes. If you want to use elements or attributes from a different namespace.
that this url ?
It isn't a URL. It just looks like one to help avoid two people using the same name for their namespace.

Related

Android Layout - when to use app: vs android:?

I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!
You can use the app namespace to have app compatibility with older API versions.
For example
app:srcCompat="#drawable/customborder" has the same effects with
android:background="#drawable/customborder"
The difference is that the first will work correctly with older API's and the second will not display what you would like.
You are talking about custom namespace.In android we can create custom views in additional to already available views.
As per in Google developer docs..
To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:
Define custom attributes for your view in a resource element
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]
So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.
Refer this
If you take a look at the beginning of the your layout xml files (in which you used app:) you will (probably) find lines like this:
<?xml version="1.0" encoding="utf-8"?>
<SOME_LAYOUT xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
in this case app: namespace will be used for custom attributes, specified by you inside attrs.xml file or by someone else in one of used libraries.
Sometime the property with android only available in new Android version like
In this case, you should use app:... to make it work with older version.
moreover you will find two variants
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/[packagename]"
the difference between xmlns lines is res-auto take care of resolving our package as sometime we will add .debug or .test in our package and we already provided the packageid of the app Ex:
xmlns:app="http://schemas.android.com/apk/com.test.io.debug"
xmlns:app="http://schemas.android.com/apk/com.test.io.test"

android layout id naming: use of R.group instead of R.id

I've seen that all the layout ids are identified by #+id/viewid, but it seems the #+my_group_name/viewid works as well. Is it normal to use this naming convention, or the id must be in the "id" class ? I've checked the forum and found prefix based naming conventions but nothing like this.
In examples: I have a dialog layout called dlgeffect. In this layout:
<CheckedTextView
android:id="#+dlgeffect/text"
android:layout_width="0dip" ... >
I now the id's are reused, but during layout modifications if the ids are unique for the whole project the compiler gives error this way (and not runtime error)
Thanks,
Gzp
EDIT: and of course from java it is referenced as R.dlgeffect.text
I really dont know if you can do this, but always stick to standards, if you want it for reuse porpuse or setting property for more than element at once you can use same id for different element

Do namespaced id's exist or what is android:id from the XML point of view?

I built a Python script (with minidom) to manipulate the layout of an Android app, which is defined in an XML file. Widgets are elements in the android namespace, for example:
<TextView android:id="#+id/myId" android:text="..." />
Note the android:id attribute and its required format #+id/.... I didn't find a way to pick that element with getElementById(). Maybe it's because I don't know minidom, but more likely it's because I don't understand XML namespaces deeply.
I remember that id is an attribute to uniquely identify an element in a tree. I never saw a namespaced ID, so my guess is that android:id isn't really an ID, but just an attribute which happens to be called id in the android namespace. In this case there's no way to getElementById, because it works with XML id's. Currently I traverse the dom and check the android:id attribute to get my target element, but this technique is obviously less flexible than getElementById directly
Disclaimer: I tagged this with xslt because I think that xslt people can know the answer since they are used to working with XML and namespaces
OK - I found something else that may be of help to you. Apparently it depends on what has been listed of being of type "ID". Where is that defin ed and can you influence it? I don't know :-( But check How to set element's id in Python's xml.dom.minidom?
See extract below from http://www.w3.org/TR/DOM-Level-2-Core/core.html
getElementById introduced in DOM Level 2
Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID.
Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.
Yes, attributes can have namespaces. The best known example of attribute namespace is the namespace declaration itself:
xmlns:someNamespace="someUri"
The only advantage in using namespaces that I see, is that the same attribute name may be used more than once, when assigned separate namespaces, such as in
<x xmlns:n1="http://www.w3.org"
xmlns:n2="http://www.my.org" >
<good n1:a="1" n2:a="2" />
</x>
Not sure what one would need that for though...
Now for selecting namespaced attributes.
In XSLT/Xpath this is done in normal fashion, including the namespace of course; don't forget the # character. Example for the above XML:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n1="http://www.w3.org"
xmlns:n2="http://www.my.org">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="//good">
<root>
<n1><xsl:value-of select="./#n1:a"/></n1>
<n2><xsl:value-of select="./#n2:a"/></n2>
</root>
</xsl:template>
</xsl:stylesheet>
gives
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:n1="http://www.w3.org" xmlns:n2="http://www.my.org">
<n1>1</n1>
<n2>2</n2>
</root>

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Define custom Android components using short namespace?

Working on my first Android application. I'm wondering if there's a way to use the xmlns in the markup in any way. In Flex, for example, I can define a namespace:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cb="com.typeoneerror.apps.app.views.components.*">
<cb:CustomComponent paramName="demo"></cb:CustomComponent>
</mx:VBox>
Android seems to be slightly different. You use the namespace when defining params but not the tag itself. This is a bit wordy to me, so I'm wondering if there's a way to configure or change this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cb="http://schemas.android.com/apk/res/com.typeoneerror.apps.app">
<com.typeoneerror.apps.app.views.components.CustomComponent cb:paramName="demo"/>
</LinearLayout>
I'd like to use
<cb:CustomComponent cb:paramName="demo"></cb:CustomComponent>
Possible?
No, sorry. The element name is a Java class name, and in the case of custom widgets, is a fully-qualified class name.
I have seen some syntax where the element name is View and there is a class attribute with the widget's class name. I can't find that in the docs and don't have an sample available, though.

Categories

Resources