7

I'm using a shape element to put as the background of a view, doing as this SO question. I'm using Android Studio and it tells me that...

Element shape doesn't have required attribute android:layout_height

Element shape doesn't have required attribute android:layout_width

The code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/darkBrown"/>
<stroke android:layout_width="match_parent" android:layout_height="match_parent"/>
<corners android:radius="10dip"/>
<padding android:left="0dip" android:right="0dip" android:top="0dip" android:bottom="0dip"/>

</shape>

So basically my question: why do I get this error if shapes technically don't need these attributes? How can I tell android to stop marking it as an error or how can I avoid it?

Thank you.

SOLVED: The reason was because it wasn't inside the drawable folder.

EDIT: Yes I'm aware the stroke part is incorrect.

Community
  • 1
  • 1
Lorenz
  • 638
  • 5
  • 13

3 Answers3

27

The reason the error was shown is because it wasn't inside the drawable folder. (It moved automatically to layout when I renamed the drawable folder after a typo.)

Lorenz
  • 638
  • 5
  • 13
1

Here is an example of what a shape should look like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
    android:radius="14dp"
    />
<solid
    android:color="#ADD8E6"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="100dp"
    android:height="60dp"
    />
<stroke
    android:width="3dp"
    android:color="#ADD8E6"
    />

</shape>
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • Thanks for the answer! But I still can't get rid of the red! Maybe it's an Android Studio thing? ![Screenie here](http://i.gyazo.com/c7d53f389ae1f3c503b10b253afb5ab0.png) EDIT: Okay my bad, for some reason the layout_bg.xml moved to layout when I renamed the drawable folder! – Lorenz Jul 31 '15 at 18:08
0

You drawable XML is not correct in the first place. You cannot just use attributes you like and expect it work, for example your <stroke> has android:layout_width and android:layout_height which makes no sense for <stroke> and is not supported. It also android:width though, but this is different width and got NOTHING in common with layout_width. Docs are here and include all attributes supported.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • That has nothing to do with the error, because I still get it even after using technically correct attributes for width and height (copied the code provided by the answer above you). – Lorenz Jul 31 '15 at 18:06