title: Android View, creating custom component
body: <p>First of all, I know there is a lot of similar questions, but i could find a complete answer to do what I want. It's about the creation of a custom component.</p>
  
  <p>Thing is, I would like to create a custom animated component for my UI. Moreover, the client would like to easily change the image of the component so I decided to go for an xml layout which is animated in the code. But It's seems that I can't get it right...</p>
  
  <p>The exemple for my first try is a needle gauge which indicate the C02 level. </p>
  
  <p>There is 2 images : the gauge and the needle, so I created a gauge layout with my two ImageView.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;com.erdf.components 
      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/gauge"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:src="@drawable/jauge" /&gt;
  
  &lt;ImageView
      android:id="@+id/needle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:src="@drawable/jauge_needle" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Then I created the associated class : </p>
  
  <pre><code>public class components extends RelativeLayout {
  
      private ImageView mNeedle;
  
      private int mValue = 0;
  
      public components(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
  
          mNeedle = (ImageView)findViewById(R.id.needle);
      }
  
  
      public void SetGaugeValue(int value) {
          if(value &gt; 180) {
              value = 180;
          }
          else if(value &lt; -180)
          {
              value = -180;
          }
  
          RotateAnimation needleDeflection = new RotateAnimation(mValue, value, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
          needleDeflection.setDuration(100*(Math.abs(mValue-value))); 
          needleDeflection.setFillAfter(true);
  
          mNeedle.startAnimation(needleDeflection);
  
      }
  }
  </code></pre>
  
  <p>Finally I tried to include this compnent into my ActivityView </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;include
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          layout="@layout/gauge_layout" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here's the code of the activity Class :</p>
  
  <pre><code>public class GaugeActivity extends Activity {
  
      private components test;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.view_layout);
      }
  }
  </code></pre>
  
  <p>So my first question is : why do i do wrong to get this exception at the execution of the application :</p>
  
  <pre><code>    01-29 16:04:28.886: E/AndroidRuntime(1570): FATAL EXCEPTION: main
  01-29 16:04:28.886: E/AndroidRuntime(1570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gauge/com.example.gauge.GaugeActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.os.Looper.loop(Looper.java:137)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.main(ActivityThread.java:5039)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invoke(Method.java:511)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at dalvik.system.NativeStart.main(Native Method)
  01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:807)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Activity.setContentView(Activity.java:1881)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.example.gauge.GaugeActivity.onCreate(GaugeActivity.java:24)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Activity.performCreate(Activity.java:5104)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     ... 11 more
  01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android.util.AttributeSet]
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.Class.getConstructor(Class.java:431)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
  01-29 16:04:28.886: E/AndroidRuntime(1570):     ... 23 more
  </code></pre>
  
  <p>The seconde one is about how to use my component. How do I cast the included layout in my member variable test which is an instance of the class associated to the same included layout ? Then I could simply use the setGaugeValue ( ) function to animating the needle.</p>
  
  <p>I hope i was clear enough.</p>