Search code examples
androidlistviewkeyboardtouchscreen

Android: how to allow the navigation in a ListView on non-touchscreen devices?


I would like to get rid of the Android Market filter android.hardware.touchscreen in order to allow the download of my app on non-touchscreen devices (device with keyboard navigatio only).

There are not so much devices which are not touchscreen but there are some...

My app works with keyboard except the ListViews... I would like the user to be able to navigate in my ListView only with the keyboard. Namely, he must be able to scroll in the listView => the different cells have to be highlighted successively when he scrolls.

How to do that ?

Thanks !!


Solution

  • Maybe you can create a onKeyListener. It goes something like this:

    private class myListKeyListener implements onKeyListener {
        @Override
        public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
    
            if (KeyEvent.ACTION_DOWN == arg2.getAction()) {
                switch (arg1) {
                case KeyEvent.KEYCODE_DPAD_UP: {
    
                        int i = list.getSelectedItemPosition();
                        if (i > 0)
                            i--;
                        list.setSelection(i);
                        break;}
    }
    

    the same thing for DOWN, RIGHT, LEFT...you can do what you want with it

    After that attach listener to your list in onCreate method:

    list.setOnKeyListner(new MyListKeyListener);

    Hope it helps you ;)