efpies
efpies

Reputation: 3725

Custom RelativeLayouts in layout.xml

I have a custom RelativeLayout in remote_image_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</RelativeLayout>

Okay, now I want to include it in main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <Button android:text="Start Download" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="onClick" />
    <Button android:text="View Downloads" android:id="@+id/button2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="showDownload" />
    <ScrollView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        >
        <LinearLayout
                android:id="@+id/lay"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="vertical"
                >
            <include layout="@layout/remote_image_view" android:id="@+id/imageView1" />
            <include layout="@layout/remote_image_view" android:id="@+id/imageView2"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView3"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView4"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView5"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView6"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

That's good. So I created a class RemoteImage:

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);
    }
}

And there a wild question appears. I cannot write code as below:

RemoteImageView rm1 = (RemoteImageView)findViewById(R.id.imageView1);

Because I'll get exception:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.dmact.remoteimage.RemoteImageView

So I should create an instance of RemoteImageView dynamically and then apply some LayoutParams to it. Is there a way not to do this? I want to make the layout in XML, not in code.

Upvotes: 1

Views: 4236

Answers (2)

dymmeh
dymmeh

Reputation: 22306

If you need to subclass RelativeLayout using your RemoteImageView class you must also specify your class in the XML file

ex

<?xml version="1.0" encoding="utf-8"?>
<com.yourpackagepath.RemoteImageView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</com.yourpackagepath.RemoteImageView>

Upvotes: 7

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

give the complete path your java file instead of RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</RelativeLayout>

like

<com....RemoteImageView  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
        <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/placeholder"
            />
    </com....RemoteImageView>

Upvotes: 1

Related Questions