title: Adding touch effect in custom View - Android
body: <p>I've created a custom <code>View</code> with a text inside:</p>
  
  <pre><code>canvas.drawText(text,
              bounds.centerX(),
              _getTextVerticalCenterPosition(bounds, paint),
              paint);
  </code></pre>
  
  <p><em><code>_getTextVerticalCenterPosition</code> just returns y-position.</em></p>
  
  <p>Now on touch (<code>onTouchEvent</code>) I'd like to set any effect for user to visually feel the touch but have no idea where to start. <code>MotionEvent.ACTION_DOWN</code> should initiate the effect and <code>MotionEvent.ACTION_UP</code> should end it and return the text to its initial look. And what about "sliding" the finger on the screen. It'd be great if the text's initial look would be returned right after finger leaving the bounds not after the <code>MotionEvent.ACTION_UP</code>.</p>
  
  <pre><code>@Override
  public boolean onTouchEvent(MotionEvent event)
  {
      float x = event.getX();
      float y = event.getY();
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              if (bounds.contains(x, y)) {
                  // Text pressed
  
              }
          }
      }
      return true;
  }
  </code></pre>
  
  <p>Or is it possible to use basic views, such as <code>Button</code>, in my custom one and combine them with arcs and paths?</p>
  
  <hr>
  
  <p><strong>Solution</strong>:</p>
  
  <pre><code>@Override
  public boolean onTouchEvent(MotionEvent event)
  {
      float x = event.getX();
      float y = event.getY();
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              if (bounds.contains(x, y)) {
                  // Text pressed
                  _setTextColor(mOnTouchColor);
                  invalidate();
              }
              break;
          }
          case MotionEvent.ACTION_MOVE: {
              _setTextColor(mColor);
              invalidate();
              break;
          }
          case MotionEvent.ACTION_MOVE: {
              // Finger off the bounds
              if (!bounds.contains(x, y)) {
                  _setTextColor(mColor);
                  invalidate();
              }
              break;
          }
      }
      return true;
  }
  </code></pre>