using remote control for android TV application

6.7k views Asked by At

I'm developing an Android application for TV when I'm trying to test it in the real Android TV I can't navigate to any button or anything using the remote control buttons up, down, left, right.

I have searched about it and I found that I have to use (Enable D-pad Navigation ) but I don't know how to use this , I didn't find any code or tutorial for it My application is so simple. It makes dynamic layout beside each other and each layout has only one button and one recycle view.

2

There are 2 answers

0
shalafi On BEST ANSWER

By default Android implements basic D-Pad navigation inferred from the layout distribution. It works quite well if you use mostly lists or linear layouts.

The key difference is that the views are on the focused state instead of selected, so if you are using custom background i.e. for buttons it may look like it is not working while in fact it is.

You can try it out on an emulator using the cursor keys on the keyboard.

Alternatively or if the inferred navigation does not work you can define the nextFocusUp, Down, Left and Right per view.

It is all quite well explained on the official documentation: https://developer.android.com/training/tv/start/navigation.html

0
Muhammed Haris On

try this,

@Override
    public boolean onKeyDown(int keyCode, KeyEvent events) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.onKeyDown(keyCode, events);
    }

if in case call is not getInside above ,then try this:

  @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.dispatchKeyEvent(event);
    }