Elad Benda Elad Benda - 3 months ago 34
Android Question

Binary XML file line #7: You must supply a layout_width attribute

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/off_background">

<ImageView
android:id="@+id/bottom_layer"
android:src="@drawable/handle"/>

<ImageView
android:id="@+id/upper_layer"
android:src="@drawable/switch_v"/>

</FrameLayout>


whenever this code is executed:

inflater.inflate(R.layout.settings_switch, this);


I get this error:

10-29 13:27:00.090: E/AndroidRuntime(22364): Caused by: java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.


how can it be?

I have

android:layout_width="match_parent"
android:layout_height="match_parent"

Answer

Add these attributes to imageview's

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  // can use dp like 20dp instead of wrap_content

All view groups include a width and height (layout_width and layout_height), and each view is required to define them.

wrap_content tells your view to size itself to the dimensions required by its content fill_parent (renamed match_parent in API Level 8) tells your view to become as big as its parent view group will allow.

In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or fill_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes. The accepted measurement types are defined in the Available Resources document.

Check the link for more info

http://developer.android.com/guide/topics/ui/declaring-layout.html

Comments