title: how to use custom ImageView
body: <p>I have found the following code on how to get display height and width from ImageView: <a href="https://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview">Trying to get the display size of an image in an ImageView</a> . When I try to use it in the layout snippet below, I get following error.</p>
  
  <pre><code> &lt;com.mycompany.widget.SizeAwareImageView
                  android:id="@+id/account_profile_img"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:background="@drawable/avatar"
                  android:clickable="true"
                  android:gravity="bottom|center_horizontal"
                  android:onClick="onEditPhotoClicked" /&gt;
  </code></pre>
  
  <p>ERROR:</p>
  
  <pre><code>FATAL EXCEPTION: main
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myapp/com.company.myapp.RegisterActivity}: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
      at android.app.ActivityThread.access$700(ActivityThread.java:152)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5328)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
      at android.view.LayoutInflater.createView(LayoutInflater.java:602)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
      at android.app.Activity.setContentView(Activity.java:1928)
      at com.company.myapp.RegisterActivity.onCreate(RegisterActivity.java:63)
      at android.app.Activity.performCreate(Activity.java:5250)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      ... 11 more
   Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android.util.AttributeSet]
      at java.lang.Class.getConstructorOrMethod(Class.java:460)
      at java.lang.Class.getConstructor(Class.java:431)
      at android.view.LayoutInflater.createView(LayoutInflater.java:567)
      ... 25 more
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>From the graphics Layout I also get the error</p>
  
  <pre><code>java.lang.NullPointerException
  Exception details are logged in Window &gt; Show View &gt; Error Log
  
  java.lang.NullPointerException
      at com.company.myapp.widget.SizeAwareImageView.onMeasure(SizeAwareImageView.java:66)
      at android.view.View.measure(View.java:15848)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
      at android.view.View.measure(View.java:15848)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
      at android.view.View.measure(View.java:15848)
      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
      at android.view.View.measure(View.java:15848)
      at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
      at android.widget.ScrollView.onMeasure(ScrollView.java:321)
      at android.view.View.measure(View.java:15848)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
      at android.view.View.measure(View.java:15848)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
      at android.view.View.measure(View.java:15848)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
      at android.view.View.measure(View.java:15848)
  </code></pre>
  
  <p><strong>SECOND UPDATE</strong></p>
  
  <p>the errored line in question is the second of</p>
  
  <pre><code>final Drawable d = getDrawable();
  final int origW = d.getIntrinsicWidth();
  final int origH = d.getIntrinsicHeight();
  </code></pre>
  
  <p>The complete code is here on stack overflow at <a href="https://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview">Trying to get the display size of an image in an ImageView</a> . It's the response with the most votes.</p>