title: Need the image id from xml attributes custom widget
body: <p>I have a custom control (very simple for now) that is like a button.  It needs to display an unpressed and a pressed image.  It appears multiple times in the activity and has different pairs of images depending on where it's used.  Think of toolbar icons - similar to that.</p>
  
  <p>Here's an extract of my layout:</p>
  
  <pre><code>&lt;TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:MyApp="http://schemas.android.com/apk/res/com.example.mockup"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" &gt;
  
    &lt;TableRow&gt;
      &lt;com.example.mockup.ImageGestureButton
        android:id="@+id/parent_arrow"
        android:src="@drawable/parent_arrow"
        MyApp:srcPressed="@drawable/parent_arrow_pressed"
        ... /&gt;
       ...
    &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>attrs.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;resources&gt; 
      &lt;declare-styleable name="ImageGestureButton"&gt; 
          &lt;attr name="srcPressed" format="reference" /&gt; 
      &lt;/declare-styleable&gt; 
  &lt;/resources&gt; 
  </code></pre>
  
  <p>And, in the R.java, one finds:</p>
  
  <pre><code>public static final class drawable {
      public static final int parent_arrow=0x7f020003;
      public static final int parent_arrow_pressed=0x7f020004;
      ...
  }
  </code></pre>
  
  <p>During widget instantiation, I want to determine the ids declared in the activity xml.  How do I do that?  I've tried this (I updated my original post with working code; so, the following works.)</p>
  
  <pre><code>public class ImageGestureButton extends ImageView
     implements View.OnTouchListener
  {
    private Drawable unpressedImage;
    private Drawable pressedImage;
  
    public ImageGestureButton (Context context, AttributeSet attrs)
    {
      super(context, attrs);
      setOnTouchListener (this);
  
      unpressedImage = getDrawable();
  
      TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.ImageGestureButton, 0, 0);
      pressedImage = a.getDrawable (R.styleable.ImageGestureButton_srcPressed);
    }
  
    public boolean onTouch (View v, MotionEvent e)
    {
      if (e.getAction() == MotionEvent.ACTION_DOWN)
      {
        setImageDrawable (pressedImage);
      }
      else if (e.getAction() == MotionEvent.ACTION_UP)
      {
        setImageDrawable (unpressedImage);
      }
  
      return false;
    }
  }
  </code></pre>