The attribute is not declared (Android) on basic xml attributes

27,661

Solution 1

Problem

Intellisense could not pick the attributes we type although those attributes are existing in android SDK and shows this Attribute is not declared.

Solution

I got this problem yesterday in Visual Studio 2015 and started searching about this, ultimately I found that these two files are missing in XML schema folder in Visual Studio, so download these files links given below,

  1. https://github.com/atsushieno/monodroid-schema-gen/blob/master/android-layout-xml.xsd
  2. https://github.com/atsushieno/monodroid-schema-gen/blob/master/schemas.android.com.apk.res.android.xsd

Just download and move files manually to C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas or simply just add these schemas within Visual Studio. This error will be gone, I resolved this issue with this procedure.

Solution 2

As I know, those attributes (that you pointed out above) aren't defined in jdk_1.7.

Use jdk_1.8 instead.

You can configure the path to the JDK in the following way:

Microsoft Visual Studio 2015 -> Tools -> Options -> Xamarin -> Android Settings -> JDK Location [Change]

Remark

If you are using Microsoft Visual Studio 2015 Update 3 or one of the earlier versions, I assure you that the warning won't disappear... this is already the Intellisense's issue (not the JDK's). It has to be fixed in the future release.

Screenshot

Solution 3

Create a template-based Xamarin.Android app.

Taking the vector.xml example from the Google/Android VectorDrawable docs and creating that as a .xml file under the Resources/drawable directory in the project:

<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="64dp"
     android:width="64dp"
     android:viewportHeight="600"
     android:viewportWidth="600" >
     <group
         android:name="rotationGroup"
         android:pivotX="300.0"
         android:pivotY="300.0"
         android:rotation="45.0" >
         <path
             android:name="v"
             android:fillColor="#000000"
             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
     </group>
 </vector>

Update your Resouce/layout/Main.axml to include the vector on the background of the LinearLayout and the Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:background="@drawable/vector"
        >
    <Button
        android:id="@+id/myButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:background="@drawable/vector"
        />
</LinearLayout>

Results in:

enter image description here

Not a pretty example from Google, but it works.

Cleaning up your vector and setting the color to red

<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="256dp"
     android:width="256dp"
     android:viewportHeight="60"
     android:viewportWidth="60" >
     <group
         android:name="rotationGroup"
         android:pivotX="0.0"
         android:pivotY="0.0"
         android:rotation="0.0" >
        <path
            android:name="so"
            android:fillColor="#F44336"
            android:pathData="M20.5,9.5
              c-1.955,0,-3.83,1.268,-4.5,3
              c-0.67,-1.732,-2.547,-3,-4.5,-3
              C8.957,9.5,7,11.432,7,14
              c0,3.53,3.793,6.257,9,11.5
              c5.207,-5.242,9,-7.97,9,-11.5
              C25,11.432,23.043,9.5,20.5,9.5z" />
     </group>
 </vector>

Results in:

enter image description here

Share:
27,661

Related videos on Youtube

LaughingMan
Author by

LaughingMan

Computer science student with an education in website development. My Main interests are the web; developing websites, mobile applications and eventually games. I work at Alinea, a company making products for elementary school kids (books as well as digital products)

Updated on January 05, 2020

Comments

  • LaughingMan
    LaughingMan over 4 years

    I'm new to this Android development, and I find the layout of it really confusing. I'm trying to have a background image on a view, and I've tried using this example Add a background image to shape in xml Android, but it looks very bad (bitmaps you know)

    So I thought vectors could be fun. Only problem is that I cannot get even the official examples to work. I've tried setting this as a background

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="256dp"
        android:width="256dp"
        android:viewportWidth="32"
        android:viewportHeight="32">
    <path android:fillColor="#8fff" android:pathData="M20.5,9.5
                      c-1.955,0,-3.83,1.268,-4.5,3
                      c-0.67,-1.732,-2.547,-3,-4.5,-3
                      C8.957,9.5,7,11.432,7,14
                      c0,3.53,3.793,6.257,9,11.5
                      c5.207,-5.242,9,-7.97,9,-11.5
                      C25,11.432,23.043,9.5,20.5,9.5z" />
    

    It renders in the design view and all, but the attributes viewportWidth, viewportHeight, fillColor and pathData all show the same warning:

    The 'http://schemas.android.com/apk/res/android:viewportWidth' is not declared
    

    If I check the file, sure enough, it's not there. Does that mean I have to explicitly declare all those types? It seems a bit odd for vanilla examples.

    Note that if I remove the 'android' in front of the warnings, it will remove warnings but still give me the same deployment error

    Android.Views.InflateException: Binary XML file line #1: Error inflating class <unknown>
    
    • SushiHangover
      SushiHangover about 8 years
      What version of Xamarin for Visual Studio are you running? And which version of Visual Studio? Which Android SDK versions?
    • LaughingMan
      LaughingMan about 8 years
      @SushiHangover Visual studio 2015 14.0.25123, Xamarin 4.0.3, Xamarin.Android 6.0.3.5 Seems that the latest SDK i have installed is API 22 (5.1.1)
    • LaughingMan
      LaughingMan about 8 years
      @SushiHangover I have now upgraded to the latest SDKs, but alas, nothing has changed
  • LaughingMan
    LaughingMan about 8 years
    I have tried that as well, the problem is that visual studio won't recognize the tag attributes. I've edited my question and uploaded a screenshot so you can see what I'm talking about
  • Esteban Verbel
    Esteban Verbel over 7 years
    I still get the warning "attribute not declared", but now the project runs. Before I was unable to deploy it
  • AlexMelw
    AlexMelw over 7 years
    @Banana, I'm glad that I could help you! But the warning won't disappear... this is already the Intellisense's issue (not the JDK's). It has to be fixed in the future release.
  • JustAPup
    JustAPup over 7 years
    Mine uses jdk1.8.0_101 and it still gives me this warning, and won't display properly in design mode. Do you know why so?
  • Olumide Oyetoke
    Olumide Oyetoke over 7 years
    I think this is the best solution. Worked for me.
  • AlexMelw
    AlexMelw over 7 years
    @Minh I'd switched to native Android Studio. Xamarin is still green and buggy.
  • γηράσκω δ' αεί πολλά διδασκόμε
    γηράσκω δ' αεί πολλά διδασκόμε over 7 years
    You just saved me. Nice sir!
  • RogerW
    RogerW over 7 years
    Just installed VS 2017. Was hoping this would have been sorted out. Same issues.
  • aelveborn
    aelveborn over 7 years
    This fixes some of the errors but not all of them (e.g. http://schemas.android.com/apk/res/android:gravity attribute is still not found, or the WebView element). There must be more schemas to download.
  • aelveborn
    aelveborn over 7 years
    @Andrey-WD The issue is specific to the Xamarin for VS addon. Xamarin Studio does not have this issue.
  • Jamshaid K.
    Jamshaid K. about 7 years
    I placed both of these files but to no avail. The errors are still there,
  • SHM
    SHM about 7 years
    in vs2017 you should place these two files under %projectDirectory%/obj\Debug\Schemas\19
  • Pete
    Pete about 7 years
    you have to add those two files. in the toolbar in VS 2015 select XML -> Schemas.. -> Add. I checked this issue in VS 2017, the error did not occure there.
  • Ashutosh Srivastava
    Ashutosh Srivastava about 7 years
    I am facing the same issue but not able to get rid of it. I tried your solution but didnt worked
  • Pyd
    Pyd over 6 years
    there are no folders xml\schemas under C:\Program Files (x86)\Microsoft Visual Studio 14.0. I created the folders and copied both the files but still nothing happens. My designed app is not showing, emulator is blank and I am getting there were deployment errors
  • omadawn
    omadawn over 6 years
    Even though my 2017 was installed fresh just few days ago (dec 2017), I ran into this problem. The 2 files were indeed missing and your fix worked.
  • Michael Bedford
    Michael Bedford over 6 years
    To all currently reading this thread, I recently installed VS2017 fresh and made sure to select the Xamarin work flow on install. However, as noted in this answer, the two files linked above are NOT installed for you in the folder. I tried to simply copy them in to the folder and that did not work. I then did what Pete said and selected XML > Schemas > Add and added the two files that way and it worked. So, it seems you must actually add them instead of copy the files in to the folder?
  • Yegor
    Yegor over 6 years
    The files in this answer seem to be horribly outdated.