Oct 8, 2013

[android] How to add listener to hardware button.

How to add listener to hardware menu button…
It is simple and easy.  Just override it. 
    @Override
    public boolean onKeyDown(int keycode,  KeyEvent e)
    {
        switch(keycode)
        {
        case KeyEvent.KEYCODE_MENU:
            Toast.makeText(this,  "Menu Button", Toast.LENGTH_SHORT).show(); 
            return true;           
        case KeyEvent.KEYCODE_BACK:
           Toast.makeText(this,  "Back Button", Toast.LENGTH_SHORT).show();  
            return true;           
        case KeyEvent.KEYCODE_HOME:
           Toast.makeText(this,  "Home Button", Toast.LENGTH_SHORT).show();  
            return true;           
        }     
        return super.onKeyDown(keycode,  e);
    }

No comments:

Post a Comment