title: How to implement ripple effect in Android KeyboardView?
body: <p>I've been trying to find a way to implement ripple effect on KeyboardView keys when they are pressed. Sounds simple enough, but I've tried all the ways of adding ripple that work on other types of view (listview, button, etc) with no success at all.</p>
  
  <p>My aim is to create a numeric keypad that looks like the keypad in the stock Calculator app which comes by default in Lollipop OS: </p>
  
  <p><img src="https://i.stack.imgur.com/fusqT.png" alt="Stock Lollipop calculator"></p>
  
  <p>There's a similar calculator app in GitHub (<a href="https://github.com/numixproject/com.numix.calculator" rel="nofollow noreferrer">https://github.com/numixproject/com.numix.calculator</a>) which has a ripple effect on its keypad, but as I read the code seems like it's using buttons for the numeric keys instead of a KeyboardView. </p>
  
  <p>I hope ripple effect is doable with KeyboardView, as my app already has the implementation of a custom numeric keyboard using KeyboardView, and I'd hate to have to change it to use buttons.</p>
  
  <p>I've tried adding the ripple as the <code>keyBackground</code> attribute from the style like this:</p>
  
  <pre><code>&lt;android.inputmethodservice.KeyboardView
      android:id="@+id/numeric_keypad"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:focusable="true"
      android:focusableInTouchMode="true"
      style="@style/my_numeric_keypad" /&gt;
  </code></pre>
  
  <p>Then in themes.xml:</p>
  
  <pre><code>&lt;style name="my_numeric_keypad"&gt;
      &lt;item name="android:keyTextSize"&gt;30dp&lt;/item&gt;
      &lt;item name="android:fontFamily"&gt;roboto&lt;/item&gt;
      &lt;item name="android:keyBackground"&gt;@drawable/numeric_keypad_ripple&lt;/item&gt;
      &lt;item name="android:keyTextColor"&gt;@android:color/white&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>and then numeric_keypad_ripple.xml in drawable-v21 folder:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;ripple 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:color="?android:colorControlHighlight"&gt;
      &lt;item android:drawable="@drawable/numeric_keypad_states"/&gt;
  &lt;/ripple&gt;
  </code></pre>
  
  <p>numeric_keypad_states.xml is the old selector with the pressed state (it used to be declared straight as the <code>keyBackground</code> attribute):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:state_pressed="true" android:drawable="@drawable/numeric_keypad_pressed" /&gt;
      &lt;item android:drawable="@drawable/numeric_keypad_normal" /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>numeric_keypad_pressed.xml and numeric_keypad_normal.xml are just the drawable with the color for each specific state, like this (both are exactly the same, just differ in the color attribute):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt;
      &lt;item&gt;
          &lt;shape android:shape="rectangle"&gt;
              &lt;solid android:color="@color/numeric_keypad_pressed_color"/&gt;
          &lt;/shape&gt;
      &lt;/item&gt;
  &lt;/layer-list&gt;
  </code></pre>
  
  <p>I thought my approach above would work; but it doesn't. In my Lollipop device, when I press the keypad it just shows the normal pressed color without any ripple at all, no difference to my old implementation without the ripple. I've tried removing the layer because I thought it's somehow overlapping with the ripple, but still doesn't work. Adding mask to the ripple also doesn't work.</p>
  
  <p>I've also tried using the rippledrawable as the pressed state drawable in the selector instead of wrapping the selector, but it's still not working. Also tried using <code>?android:attr/selectableItemBackground</code> instead of a rippledrawable but also doesn't work.</p>
  
  <p>And oh, I'm actually developing the app on Xamarin instead of native Android, but that shouldn't make any difference I suppose.</p>