26

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>
Community
  • 1
  • 1
LaughingMan
  • 640
  • 1
  • 9
  • 18
  • 1
    What version of `Xamarin` for Visual Studio are you running? And which version of `Visual Studio`? Which `Android SDK` versions? – SushiHangover May 05 '16 at 17:18
  • @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 May 05 '16 at 17:46
  • @SushiHangover I have now upgraded to the latest SDKs, but alas, nothing has changed – LaughingMan May 05 '16 at 20:21

3 Answers3

43

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.

Usman
  • 2,547
  • 31
  • 35
  • 1
    I think this is the best solution. Worked for me. – Olumide Oyetoke Feb 26 '17 at 11:02
  • 7
    Just installed VS 2017. Was hoping this would have been sorted out. Same issues. – RogerW Mar 12 '17 at 16:56
  • 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. – GSerg Mar 19 '17 at 15:12
  • I placed both of these files but to no avail. The errors are still there, – Jamshaid K. Apr 01 '17 at 23:52
  • 4
    in vs2017 you should place these two files under %projectDirectory%/obj\Debug\Schemas\19 – SHM Apr 13 '17 at 11:28
  • 6
    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. – Pete May 24 '17 at 21:38
  • I am facing the same issue but not able to get rid of it. I tried your solution but didnt worked – Ashutosh Srivastava Jun 08 '17 at 14:08
  • 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 – Pyd Oct 22 '17 at 13:58
  • 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. – omadawn Dec 11 '17 at 20:39
  • 1
    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? – Michael Bedford Jan 03 '18 at 16:57
  • 4
    The files in this answer seem to be horribly outdated. – Yegor Feb 28 '18 at 18:29
3

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

AlexMelw
  • 2,406
  • 26
  • 35
  • 2
    I still get the warning "attribute not declared", but now the project runs. Before I was unable to deploy it – Esteban Verbel Sep 30 '16 at 01:41
  • @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. – AlexMelw Oct 01 '16 at 20:06
  • 1
    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? – JustAPup Feb 26 '17 at 00:55
  • @Minh I'd switched to native Android Studio. Xamarin is still green and buggy. – AlexMelw Feb 26 '17 at 18:13
  • 1
    @Andrey-WD The issue is specific to the Xamarin for VS addon. Xamarin Studio does not have this issue. – GSerg Mar 19 '17 at 15:35
1

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

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 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 – LaughingMan May 05 '16 at 17:14