title: Get reference of custom view by id declared in xml from activity
body: <p>I have a custom view that extends ImageView and I use it in an XML layout like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_gravity="center"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:layout_marginTop="10dp"&gt;
  
          &lt;com.android.example.MyView
              android:id="@+id/myview1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="5dp" /&gt;
  
          &lt;com.android.example.MyView
              android:id="@+id/myview2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="5dp" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>in my activity I do the usual: setContentView(R.layout.myLayout) .</p>
  
  <p>Now, I need to get the reference of my class/View "MyView" in order to set a custom listener, but I i'm not able to get it from the id.</p>
  
  <pre><code>myview (MyView) findViewById(R.id.myview1);
  </code></pre>
  
  <p>returns null.</p>
  
  <p>I tried to look at similar issues but haven't found any that helped me.</p>
  
  <p>Please, note that if I add the View to the layout programmatically from the Activity everything is working fine, but I would like to be able to find what the issue is here.</p>
  
  <p>Thanks in advance.</p>