I am writing code to change a TextView when the volume up/down key gets pressed to update the TextView of the system volume % set.I now have this code which works once but it Overrides the functionality of up/down, is there a way to re-write it or exclude it from being overid?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//system volume
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int i = curVolume;
String aString = Integer.toString(i);
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setText(aString);
sysVol.invalidate();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Don’t return true in your code, but use
instead like in the other branches. Returning true means the event got handled properly and no further action will be taken. Returning false means the event has still to be handled.