Navigation design for TV - android tutorial (source code download)


Updated: 07 May 2013

Tested in:
ZTV - model E class - E1-1 (android 4.0.4) + Samsung TV 32"

Base on:
Eclipse Indigo build in 20120216-1857, Java 1.6, Android 4.2.2

Download Source code:
TestZtv20130507.rar

Description:
This tutorial focused on D-pad handle for TV android. If you don't have any TV android box, you can use Android Emulator with soft D-pad enabled.

Screen shot:


Tutorial:
1. To control the D-pad moving, use some attributes:
android:nextFocusUp,
android:nextFocusDown,
android:nextFocusLeft
android:nextFocusRight

2. If you want to control the items moving of gridview, modify setOnKeyListener event

gridView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    if (keyCode != KeyEvent.KEYCODE_DPAD_UP && keyCode != KeyEvent.KEYCODE_DPAD_DOWN && keyCode != KeyEvent.KEYCODE_DPAD_CENTER) {

        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == 0) {

            try {
                if (gridView.getSelectedItemPosition() < mAdapter.getCount() - 1) {
                    gridView.setSelection(gridView.getSelectedItemPosition() + 1);
                    return true;
                }
            } catch (Exception e) {
                gridView.setSelection(mAdapter.getCount() - 1);
                return true;
             }
         } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() == 0) {
            try {
                if (gridView.getSelectedItemPosition() > 0) {
                    gridView.setSelection(gridView.getSelectedItemPosition() - 1);
                return true;
            }
            } catch (Exception e) {
                gridView.setSelection(0);
                return true;
            }
         }
     }
     return false;
}
});

References:
developers.google.com/tv/android/docs/gtv_controlguide

Leave a Reply

Powered by Blogger.