title: Custom RelativeLayouts in layout.xml
body: <p>I have a custom RelativeLayout in <code>remote_image_view.xml</code>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"&gt;
      &lt;ImageView
              android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/placeholder"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Okay, now I want to include it in <code>main.xml</code>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent"&gt;
      &lt;Button android:text="Start Download" android:id="@+id/button1"
              android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:onClick="onClick" /&gt;
      &lt;Button android:text="View Downloads" android:id="@+id/button2"
              android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:onClick="showDownload" /&gt;
      &lt;ScrollView
          android:layout_height="fill_parent"
          android:layout_width="fill_parent"
          &gt;
          &lt;LinearLayout
                  android:id="@+id/lay"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:orientation="vertical"
                  &gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView1" /&gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView2"/&gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView3"/&gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView4"/&gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView5"/&gt;
              &lt;include layout="@layout/remote_image_view" android:id="@+id/imageView6"/&gt;
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>That's good. So I created a class <code>RemoteImage</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class RemoteImageView extends RelativeLayout {
  
      public RemoteImageView(Context context) {
          super(context);
      }
  
      public void init(RelativeLayout.LayoutParams layoutParams) {
          LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          inflater.inflate(R.layout.remote_image_view, this);
          setLayoutParams(layoutParams);
      }
  }
  </code></pre>
  
  <p>And there a wild question appears.
  I cannot write code as below:</p>
  
  <pre class="lang-java prettyprint-override"><code>RemoteImageView rm1 = (RemoteImageView)findViewById(R.id.imageView1);
  </code></pre>
  
  <p>Because I'll get exception:</p>
  
  <pre><code>java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.dmact.remoteimage.RemoteImageView
  </code></pre>
  
  <p>So I should create an instance of <code>RemoteImageView</code> dynamically and then apply some <code>LayoutParams</code> to it. Is there a way not to do this? I want to make the layout in XML, not in code.</p>