Layout in Android? - android

I got an error error: Error parsing XML: unbound prefix when i did not add an attribute xmlns:android="http://schemas.android.com/apk/res/android" for my Layout. Why we want to set this attribute for a layout?Any Specific Reasons? what i put the url mentioned as a value for xmlns:android. I got the server not found Exception. Please Share your knowledge about this?

This is not really an attribute for your layout but the namespace for the XML. Adding this xmlns:android would allow you to use all the "android:..." attributes and others.
WRT the 'server not found' exception, can you post the XML file ?

Matthieu explained why you need that attribute.
The reason you're getting a server not found error, is because XML namespace URLs aren't required to actually exist, and in this case they clearly don't.

Related

what is "android" in xml file? Where does definition of Button come from?

We start xml file with some layout. Within that layout we create our views like Button, TextView etc. But how does the system know what are Button and TextView? I mean we are not importing anything. Moreover inside Button, we write android:layout_width = "wrap_content" what is android in this? Since it is inside Button why can't we write layout_width = "wrap_content" directly?
From developer.android.com 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. Read XML NameSpace
Check out tutorial on namespaces
It is called as the namespace. At the top of every XML file you will have this line xmlns:android="http://schemas.android.com/apk/res/android" That's where the namespace android comes from
XML is a document language. The meaning of the elements is given to them by the compiler.
When you compile the XML file using Android's resource compiler, it imparts meaning to them.
The prefix android: establishes the namespace of the tag that follows it.
You can learn more about XML namespaces here: http://www.w3schools.com/xml/xml_namespaces.asp

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>

What is the meaning of xmlns: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.

How to get embbed XML from XML

I have a little problem, during sending request to serwer I'm getting xml with embedded xml in it. Application works on Android 2.1 so I can not just use getTextContent() to get value, so I'm using Node.getFirstChild().getNodeValue(); and for standard node with text it works fine but for node with embedded xml in it, it isn't. Does someone have any advice or had the same problem? How to solve it.
BTW: When I checked type of node all of the are 1 so it means ELEMENT_NODE.
What exactly are you receiving from server? Something like;
<?xml version="1.0"?>
<embedded-xml>
<?xml version="1.0"?>
<data/>
</embedded-xml>
Or;
<?xml version="1.0"?>
<embedded-xml>
<data/>
</embedded-xml>
In latter case you can simply retrieve data -node (not value), there's no need to parse it separately, while first case isn't exactly valid afaik.
You can't embed an arbitrary XML document in an XML document because XML documents can include sequences which are illegal with CDATA sections that are required to embed XML in the first place.
If the task is being able to embed, then extract, an arbitrary XML document, you have to encode the XML document, then embed it in a CDATA section. The reason is that the closing tag for CDATA is "]]>" and if the (arbitrary) embedded document contains a CDATA section, the "]]>" tag for that documents CDATA section will close the CDATA section in the parent.
In my experience, the best approach to embedding XML inside of XML is to compress (because Base64 encoding is going to blow it up ...) then Base64 encode. The reason I've taken this approach is because it is fool-proof and the two transforms (some kind of compressor and base64 en/de-coding) are widely available.

Categories

Resources