javaandroidandroid-studioyoutubeyoutube-channels

Linking Channel Up/Down Button with Android TV Application


I am trying to build an Android TV application using Android Studio that supports channel up/down buttons as an extra features to navigate to different YouTube channels/shows/videos (like a normal satellite TV would do) rather than only supporting the D-pad navigation buttons. Is it possible to achieve this & hopefully anyone had an idea how to do this?

Thank you.


Solution

  • While waiting for someone to reply to this question, I've discovered the solution myself.

    Basically you just need to Override onKeyDown() or onKeyUp() method in related Activity class, and don't forget to return super.onKeyDown() or super.onKeyUp() so unattended KeyEvent will be attended as normally it will.

    As for my case, I'm trying to have custom features for channel up & down buttons only. So below is the sample code.

    @Override
    public boolean onKeyDown(int KeyCode, KeyEvent event){
    
        boolean handled = false;
    
    
        if(KeyCode == KeyEvent.KEYCODE_CHANNEL_DOWN){
            Log.i("KeyEvent","Channel down button pressed");//for debugging, to be printed on logcat
            handled=true;
    
            //do something
        }
    
        else if(KeyCode == KeyEvent.KEYCODE_CHANNEL_UP){
            Log.i("KeyEvent","Channel up button pressed");//for debugging, to be printed on logcat
            handled=true;
    
            //do something
        }
    
    
        if(handled){
    
            return handled;
        }
    
        //return super.onKeyDown() to attend unattended KeyEvent
        else{
            return super.onKeyDown(KeyCode, event);
        }
    }
    

    KeyEvent class documentation that contains list of KEYCODE constants available can be found here.