title: Android Studio TV remote buttons
body: <p>I'm trying to make an app for android tv that will use the following buttons from a tv remote: <code>up</code>, <code>down</code>, <code>left</code>, <code>right</code>, <code>center/enter</code>, <code>home</code>, <code>back</code>.</p>
  
  <p>What classes/events do I need to do this? 
  I've been trying to use the <code>Dpad</code> code found here: <a href="https://developer.android.com/training/game-controllers/controller-input.html#dpad" rel="noreferrer">Link dev android.</a></p>
  
  <p>But it doesn't work when I try to test it with the android emulator on a TV with the directional pad input. With a lot of Log statements, I found my problem to be the following lines of code:</p>
  
  <pre><code>if (event instanceof MotionEvent) {
      // Use the hat axis value to find the D-pad direction
      MotionEvent motionEvent = (MotionEvent) event;
      float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
      float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
      Log.d("test", "xaxis = " + String.valueOf(xaxis) +
                    " yaxis = " + String.valueOf(yaxis));
  }
  Log.d("test", "returning directionPressed as - " +
                 String.valueOf(directionPressed));
  
  return directionPressed;
  </code></pre>
  
  <p>And the output I get is as follows (and prints 2 times, even if I press a button only once):</p>
  
  <pre><code>09-13 14:45:05.643 1489-1489/omgandroid D/test: is motion event = true
  09-13 14:45:05.643 1489-1489/omgandroid D/test: is key event = false
  09-13 14:45:05.643 1489-1489/omgandroid D/test: xaxis = 0.0 yaxis = 0.0
  09-13 14:45:05.643 1489-1489/omgandroid D/test: returning directionPressed as -1
  </code></pre>
  
  <p>I see that <code>getAxisValue(MotionEvent.AXIS_HAT_X/Y)</code> is always returning 0.0, but I don't know why. </p>
  
  <p>Here is the code where I'm calling this function in my <code>MainActivity.java</code> (inside <code>OnCreate</code>):</p>
  
  <pre><code>mContentView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
      @Override
      public boolean onGenericMotion(View view, MotionEvent event) {
          Log.d("test", "this works too");
          // Check if this event if from a D-pad and process accordingly.
          boolean check = Dpad.isDpadDevice(event);
          String str_check = String.valueOf(check);
          Log.d("test", "is dpad device? " + str_check);
          if (check) {
  
              int press = mDpad.getDirectionPressed(event);
              Log.d("test", String.valueOf(press));
              switch (press) {
                  case LEFT:
                      // Do something for LEFT direction press
                      Log.d("test", "LEFT");
                      String uri = source + image;
                      ImageView img = (ImageView) findViewById(R.id.fullscreen_content);
                      img.setImageResource(R.drawable.a00_d01_01);
                      return true;
                  case RIGHT:
                      // Do something for RIGHT direction press
                      Log.d("test", "RIGHT");
                      return true;
                  case UP:
                      // Do something for UP direction press
                      Log.d("test", "UP");
                      return true;
                  case DOWN:
                      // Do something for DOWN direction press
                      Log.d("test", "DOWN");
                      return true;
                  case CENTER:
                      // DO something for CENTER direction press
                      Log.d("test", "CENTER");
                      return true;
                  default:
                      return false;
              }
          }
          return false;
      }
  });
  </code></pre>