8

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?

3
  • @Xylian Unfortunately, not. Sorry. I ended up working around it by using custom attributes in styles.xml instead though. Commented Oct 18, 2014 at 0:52
  • See this it might help stackoverflow.com/questions/4568632/… Commented May 27, 2015 at 15:18
  • 2
    I think it is Eclipse code analizer issue. Maybe you could add exception for Eclipse somewhere in settings. You could try to build project directly from command line. I pretty sure you will have success. Also it's time to migrate to AndroidStudio vs Gradle. It is pleasure ti work with it Commented Jun 8, 2015 at 15:27

1 Answer 1

2

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:

  1. add xmlns:tools="http://schemas.android.com/tools" to your root view
  2. add tools:ignore="MissingPrefix" to your text view

On a side note, however, I usually use custom attributes with custom views :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.