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.
Related
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"
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
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
Typically I have to write layout code like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />
I want to do something like this:
<LinearLayout xmlns="http://schemas.android.com/apk/res/android"
layout_width="fill_parent"
layout_height="fill_parent"
orientation="vertical" >
But this code doesn't run properly. Why?
And second question: Why element namen are in CamelCase and attributes are in under_score?
XML default namespaces do not apply to attribute names. Hence, you always have to specify the namespace of an attribute, if it has one:
Default namespace declarations do not
apply directly to attribute names; the
interpretation of unprefixed
attributes is determined by the
element on which they appear.
So the real question is: Why did the Android designers define the element names without a namespace, putting only the attributes into the Android namespace?
As the document suggests, if the element names were in the Android namespace, then attribute names really wouldn't need their own namespace.
But this code doesn't run properly. Why?
Because the build tools do not support it. Last I checked, any prefixed namespace should work (e.g., xmlns:a="http://schemas.android.com/apk/res/android"), but the default namespace has never worked.
If you wish, you can propose and contribute a patch. Along the way, you will be able to determine whether or not there is a philosophical reason for this, a technical reason, or if they just never got around to it.
Why element namen are in CamelCase and attributes are in under_score?
Element names are Java classes, which are typically in CamelCase. Attributes are not "in under_score" in general -- the layout_ prefix indicates a family of attributes that are requests from a View to its container. But, if you look at the attributes more carefully, you will see that most are camelCase, ignoring this prefix (e.g., android:textSize).
Android layouts are defined in XML with this namespace declared in the root element:
xmlns:android="http://schemas.android.com/apk/res/android"
Example of an element:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" />
Why is the android prefix used instead of ommitting it like xmlns="http... ?
Why is the prefix used only in attributes and not in elements?
Interesting question! it sure feel a bit weird.
It was a design choice by Google to be as strict as possible on namespace to handle errors at compile time.
The prefix is not used on elements because theses are representing Java classes: com.android.widget.TextView (com.android.widget.* can always be truncated). The java namespace of this class will be automagically resolved at compile time, so an xml namespace representing a fully qualified java namespace is not welcome here. But attributes can be mapped to any of the inherited java classes of the Element. Hence the namespace on attributes to allow inheritance.
This is done like this mainly because the layout describes Java objects and Google here is using the XML namespace mechanism to help in mapping your Layout with Java objects. So there are collisions between the Java namespace world and XML namespace world.
It also allow us developers to subclass elements, add our own attributes without worrying that the next version of the platform will maybe add an attribute with the same name.
See the two replies to this blog post by Dianne Hackborn, a well-known android engineer working at google: http://www.elharo.com/blog/software-development/xml/2008/09/16/android-xml-weirdness/