title: Android: using LayoutInflater.inflate to pass custom parameters to a constructor
body: <p>This has been bothering me for a while, and none of my searching has yielded results. If I have a custom GUI element, I can use a LayoutInflater to inflate it as I would a normal component. The inflation call results in a call to my custom GUI element's constructor, and all is well. </p>
  
  <p>However, what if I want to add a custom parameter to my element's constructor? Is there a way I can pass this parameter in using LayoutInflater?</p>
  
  <p>For example: </p>
  
  <p>In main xml, I have a holder for my layout:</p>
  
  <pre><code>&lt;LinearLayout
      android:id="@+id/myFrameLayoutHolder"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and a MyFrameLayout.xml file:</p>
  
  <pre><code> &lt;com.example.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/MyFLayout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="1 &gt;
      &lt;!-- Cool custom stuff --&gt;
   &lt;/com.example.MyFrameLayout&gt;
  </code></pre>
  
  <p>and an inflater call:</p>
  
  <pre><code>LayoutInflater MyInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  LinearLayout myFLayoutHolder = (LinearLayout) findViewById(R.id.myFrameLayoutHolder);
  
  MyFrameLayout L = ((MyFrameLayout) MyInflater.inflate(R.layout.MyFLayout, myFLayoutHolder, false));
  myFLayoutHolder.addView(L);
  </code></pre>
  
  <p>If, in my class that extends FrameLayout, I add a parameter to my constructor, I get a crash:</p>
  
  <pre><code>public class MyFrameLayout extends FrameLayout {
      private int myInt;
  
      public MyFrameLayout(Context context) {
          this(context, null);
      }
  
      public MyFrameLayout(Context context, AttributeSet attrs) {
          this(context, attrs, 0, 0);
      }
  
      public MyFrameLayout(Context context, AttributeSet attrs, int defStyle, int myParameter) {
          super(context, attrs, defStyle);
          myInt = myParameter;
          //Amazing feats of initialization
      }
  }
  </code></pre>
  
  <p>Now, it's easy enough to work around this issue by defining a custom init method that I call right after layout inflation, but that seems clumsy to me. Is there a better way?</p>