I have Activityone Fragment. This snippet has one button, and I want something to happen when I press the enter button on my hardware keyboard (I am testing it with adb keyevent).
I read a lot of solutions here in stackoverflow, but none of them worked.
This is my snippet code:
btnPhoto = (Button) rootView.findViewById(R.id.taskBtnPhoto);
photoView = (ImageView) rootView.findViewById(R.id.taskPhotoView);
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});
btnPhoto.setFocusable(true);
btnPhoto.setFocusableInTouchMode(true);
btnPhoto.requestFocus();
btnPhoto.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
doFoo();
return true;
}
return false;
}
});
This code fits in part of onCreateViewmy snippet. Using debug, the event never fires when launched KeyEventfrom ADB or using the hardware keys of my phone (volume keys).
source
share