SparkyNZ
SparkyNZ

Reputation: 6676

Android XML Elements and Namespaces

Is it not possible to simplify the name of a custom element in an Activity XML file?

<com.library.CustomView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"
        android:id="@+id/myView"
        app:newAttr="value" />

Say for example I have a CustomView control. Do I always have to put "com.library." in front of "CustomView" or is it possible to use xmlns:custom in a FrameLayout so that I don't need to?

This is what I would like to see (if possible):

<CustomView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"
        android:id="@+id/myView"
        app:newAttr="value" />

Upvotes: 0

Views: 116

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

No. You must specify the fully qualified name of the custom view class. The custom attribites belong to a different namespace. So you nee to have

 http://schemas.android.com/apk/res/[your package name].

That is why you have

 xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"

It is necessary to specify fully qualified name of the custom view and hence

 <com.library.CustomView

http://developer.android.com/training/custom-views/create-view.html

The docs does not mention any other solution.

Edit:

There seems to be a workaround as mentioned in the below link. Notice comments on both answers. The authors feels there is a little overhead involved. So its left you to use the below although i recommend you to follow the above method mentioned in the docs.

Using custom Views in XML without using fully-qualified class name

Upvotes: 1

Related Questions