Poma
Poma

Reputation: 8474

Why I can't define android attributes in default namespace?

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?

Upvotes: 7

Views: 1078

Answers (2)

Dan Breslau
Dan Breslau

Reputation: 11522

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.

Upvotes: 8

CommonsWare
CommonsWare

Reputation: 1006724

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).

Upvotes: 2

Related Questions