1

I'm developing an app for android tv in which i has a listview.I want to use a D-pad for navigation between listview items.

For example if use presses the Down arrow button then next list item should get the focus. I have created a simple listview but i really need guidance in showing a D-Pad and scrolling between the items. Huge thanks in advance.

listView = (ListView) findViewById(R.id.list);
listView.setItemsCanFocus(true);
String[] values = new String[]{"Android List View",
            "Adapter implementation",
            "Simple List View In Android",
            "Create List View Android",
            "Android Example",
            "List View Source Code",
            "List View Array Adapter",
            "Android Example List View",
            "Adapter implementation",
            "Simple List View In Android",
            "Create List View Android",
            "Android Example List View"
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

    listView.setOnItemSelectedListener(new adapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            Log.e("right button", "yes");
            break;

        default:
            break;
    }
    return super.onKeyDown(keyCode, event);
}

}

Andrain
  • 872
  • 1
  • 16
  • 43
  • 1
    I think this [question](http://stackoverflow.com/questions/14392356) can help you on how listview works. I see that you already implemented the `listView.setItemsCanFocus(true)` in your code. It is the one that indicates that the views created by the ListAdapter can contain focusable items. For more info, check this [thread](http://stackoverflow.com/questions/26135674) and these documentations: [TV Navigation](https://developer.android.com/training/tv/start/navigation.html#d-pad-navigation) and [Controller Actions](https://developer.android.com/training/game-controllers/controller-input.html) – KENdi Oct 11 '16 at 14:03
  • Thank so much but I have already searched and read those links. Could you please tell me how to add dpad in the emulator. I done everything but unable to see it in my emulator. – Andrain Oct 11 '16 at 18:18
  • Check this [SO question](http://stackoverflow.com/questions/14561251/dpad-is-disabled-in-emulator), it can help you on how to show D-pad in the emulator. – KENdi Oct 12 '16 at 06:12

0 Answers0