jack
jack

Reputation: 155

Android Studio XML Requires Layout_Height and Layout_Width

I found this code while searching for a button with good graphics,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="100dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#7995A8"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>

However, when I copy and paste it into an XML file in Android Studio, it shows and error that says:

"element shape does not have required attribute android:layout_height"

and another error:

"element shape does not have required attribute android:layout_width"

I tried setting the android:layout_width="" and android:layout_heigth"" and android:layout_width"wrap_content", android:layout_heigth="wrap_content", the error is gone, but I can't set any button to have this as it's background ... so I can't use this XML this way.

What should I set them to?

Upvotes: 1

Views: 4493

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

There are no android:layout_width and android:layout_height attributes for <shape>, because <shape> does not go in a layout resource, where you are trying to put it. A <shape> goes in a drawable resource, such as in res/drawable/ in your project.

This sample project demonstrates the use of <shape> drawable resources. You will see that the <shape> elements are in the res/drawable/ directory.

You can also read more about <shape> drawable resources in the documentation.

Upvotes: 3

Related Questions