I am trying to use custom attributes in some Android layouts, but I am getting an error (from Eclipse) when I try to use a namespace prefix other than android:
in a child element. Note that it works ok when I use the custom:
namespace prefix in the root/parent element in the file, just not the child element.
For example, here's a simple layout with the custom namespace specified:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
custom:my_tag1="whatever"> <!-- compiles fine -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
custom:my_tag2="true"/> <!-- generates an error -->
</LinearLayout>
The error that Eclipse gives (only on the second attempt to use the custom:
prefix) is:
Unexpected namespace prefix "custom" found for tag ImageView.
If I make my root element an ImageView
instead of a LinearLayout
, the prefix is accepted. So it seems to be just a problem using the namespace prefix in a child element.
Also, if I try to add another xmlns:custom="http://schemas.android.com/apk/res-auto"
attribute to the ImageView, it complains as well.
If it helps, here is the attrs.xml
file I'm using with the above:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="my_tag1" format="string"/>
<attr name="my_tag2" format="boolean"/>
</resources>
I've seen some stuff online that leads me to believe what I want to do should be possible. For example, in the accepted answer here, Qberticus uses the prefix "whatever" in a child class. Similarly in the post here.
I don't get it. Is using a non-android namespace prefix just not allowed for child elements, or am I doing something wrong?
It can be ignored, I always did it and I didn't encounter any problems. To make it go away you need to set the following:
xmlns:tools="http://schemas.android.com/tools"
to your root viewtools:ignore="MissingPrefix"
to your text viewOn a side note, however, I usually use custom attributes with custom views :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With