title: How to initialize stuff that depends on the view’s attributes
body: <p>Say I have this class <code>MyView</code>:</p>
  
  <pre><code>public class MyView extends GLSurfaceView {
      private MyRenderer mRenderer;
  
      public MyView(Context ctx, AttributeSet attrs) {
          super(ctx, attrs);
          init(ctx);
      }
  
      private void init(Context ctx) {
          mRenderer = new MyRenderer(ctx);
          setRenderer(mRenderer);
      }
  
      @Override
      public void setBackground(Drawable background) {
          mRenderer.setBackground(background);
      }
  }
  </code></pre>
  
  <p>That is inflated by <code>MyActivity</code> like this:</p>
  
  <pre><code>public class MyActivity extends Activity {
      private MyView mView;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          init();
      }
  
      protected void init() {
          setContentView(R.layout.my_layout);
          mView = (MyView) findViewById(R.id.my_view);
      }
  }
  </code></pre>
  
  <p>To my knowledge <code>setBackground</code> is called by the inflater in <code>setContentView</code>, before <code>init</code> is called in <code>MyView</code>, so <code>mRenderer</code> has not been initialized yet.</p>
  
  <p>It seems like a catch 22, because the view's attributes can't be set without the view's initialization, which happens after the attributes are set.</p>
  
  <p>This is the layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:background="@drawable/background" &gt;
  
      &lt;com.developer.app.MyView
          android:id="@+id/my_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/background"
          /&gt;
  
      &lt;TextView
          android:id="@+id/left_arrow"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerVertical="true"
          android:background="@drawable/left_arrow"
          /&gt;
  
      &lt;TextView
          android:id="@+id/right_arrow"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:background="@drawable/right_arrow"
          /&gt;
  
      &lt;TextView
          android:id="@+id/home_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_marginRight="10dp"
          android:layout_marginTop="10dp"
          android:background="@drawable/home"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>