Search code examples
androidlayout-inflater

Inflating a custom control in Android


In my activity I have a custom made control. In onCreate I inflate the activity's view as normal with:

setContentView(R.layout.image_viewer);

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <MyCustomImageView
    android:id="@+id/tivImage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

And here is a reduced piece of code for MyCustomTextView:

 public class MyCustomImageView extends ImageView
 {
  Context context;

  public MyCustomImageView(Context context)
  {
    super(context);
    sharedConstructing(context);
  }

  public MyCustomImageView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    sharedConstructing(context);
  }
 }

The exception that gets generated reads:

android.view.InflateException: Binary XML file line #7: Error inflating class MyCustomImageView

Solution

  • you need to add the package name:

     <com.my.package.MyCustomTextView
        android:id="@+id/tivImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />