I'm trying to make an app for android tv that will use the following buttons from a tv remote: up
, down
, left
, right
, center/enter
, home
, back
.
What classes/events do I need to do this?
I've been trying to use the Dpad
code found here: Link dev android.
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:
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;
And the output I get is as follows (and prints 2 times, even if I press a button only once):
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
I see that getAxisValue(MotionEvent.AXIS_HAT_X/Y)
is always returning 0.0, but I don't know why.
Here is the code where I'm calling this function in my MainActivity.java
(inside OnCreate
):
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;
}
});