Sorry because my title similar with many others on StackOverFlow
but none of those solution meet with my problem.
I'm designing a layout use Relative Layout
. After design in code view, when I change to graphic view, Eclipse notices :
You must supply a layout_width attribute.
You must supply a layout_height attribute.
. When I run this program, will notice error in LogCat
Caused by: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/contact_name"
android:text="Sam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/contact_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="39256493"
android:layout_below="@+id/contact_name"
android:layout_alignLeft="@+id/contact_name"
android:layout_marginTop="5dp"/>
</RelativeLayout>
Your xmlns line should read xmlns:android="http://schemas.android.com/apk/res/android"
.
That'll probably fix your issue.
EDIT:
Quoting Google in http://developer.android.com/guide/topics/manifest/manifest-element.html:
xmlns:android Defines the Android namespace.
This attribute should always be set to "http://schemas.android.com/apk/res/android".
When you are using a XML file like the layout files you are using, the attributes you can use within that same file are defined by a schema.
xmlns is short for "XML namespace". You're defining inside your XML a namespace of keywords "android:", and that's why you need to declare all your attributes with "android:" in the beginning, such as android:layout_height
or android:layout_width
.
The namespace should be pointing to a valid schema, which should be pointing to a URLcontaining that exact schema. If the URL doesn't point to a valid schema, your XML attributes won't be recognized, which was the problem you were having.
I hope you could understand my explanation.
If you want to read more about XML namespaces and schemas, I can point you straight to W3Schools and Wikipedia: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_schema.
It might be irrelevant but namespace tag attracted my attention, as the usual one is:
xmlns:android="http://schemas.android.com/apk/res/android"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With