title: Android custom component: layout_width attribute not found
body: <p>I have a custom component.</p>
  
  <p>I always get a "You must supply a layout_width attribute." error after it's gone through the component's constructor. Maybe you can help me.</p>
  
  <p>The component's class:</p>
  
  <pre><code>public class UndoBar extends LinearLayout {
  
      private TextView messageTextView;
      private Button undoButton;
  
      public UndoBar(Context context) {
          this(context, null);
      }
  
      public UndoBar(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          inflater.inflate(R.layout.undobar, this);
  
          messageTextView = (TextView) findViewById(R.id.undobar_message);
          undoButton = (Button) findViewById(R.id.undobar_button);
  
          setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // here I set the height/width!!
          setOrientation(HORIZONTAL);
          setVisibility(GONE);
          setPadding(4, 4, 4, 4); // todo muss dp sein
          setBackgroundColor(Color.BLUE);
          setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
      }
  
      public Button getUndoButton() {
          return undoButton;
      }
  
      public TextView getMessageTextView() {
          return messageTextView;
      }
  
      public void setOnClickListener(OnClickListener l) {
          undoButton.setOnClickListener(l);
      }
  
  }
  </code></pre>
  
  <p>The component's XML file:</p>
  
  <pre><code>&lt;merge xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
      &lt;TextView
          android:id="@+id/undobar_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Deleted"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="#fff" /&gt;
  
      &lt;Button
          android:id="@+id/undobar_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="30dp"
          android:background="#808080"
          android:drawableLeft="@drawable/ic_revert"
          android:text="Undo"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="#fff" /&gt;
  
  &lt;/merge&gt;
  </code></pre>
  
  <p>This is where the component is embedded (I also tried setting the width/height of the component here, no luck):</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      ...
  
      &lt;de.myapp.android.UndoBar
          android:id="@+id/undobar" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The logcat:</p>
  
  <pre><code>01-26 17:48:19.921: E/java.lang.RuntimeException(5832): Binary XML file line #50: You must supply a layout_width attribute.
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832): java.lang.RuntimeException: Binary XML file line #50: You must supply a layout_width attribute.
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5631)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.ViewGroup$MarginLayoutParams.&lt;init&gt;(ViewGroup.java:5773)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout$LayoutParams.&lt;init&gt;(RelativeLayout.java:1154)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:992)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:70)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Activity.setContentView(Activity.java:1881)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at de.innosoft.android.activity.Einsaetze.onCreate(Einsaetze.java:61)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Activity.performCreate(Activity.java:5104)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.os.Looper.loop(Looper.java:137)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.main(ActivityThread.java:5191)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at java.lang.reflect.Method.invoke(Method.java:511)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
  01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>