I have an activity that contains two views. One view overrides onDraw, and handles the onTouchEvent correctly. But when I am trying to retrieve onKeyDown for the second view, it gives me nothing. Instead, the onKeyDown is called when I press the back button or any other keyboard button. This was tested in an emulator.
I want to retrieve the onKeyDown for my second view, which is a custom keypad view class extending LinearLayout, how can I go about doing this? I have already tried setFocusable(true) and setFocusableInTouchMode(true), but this is not working. I have also tried mySecondView.bringToFront(), also without success.
Any help would be appreciated.
EDIT - Tried adding an onClickListener to the second View in the main activity. Did not get called.
public class TrialActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d(TRIALTAG,"Inside on create"); setContentView(R.layout.trial_activity); keypadView = findViewById(R.id.keypad); selNumber = 0; keypadView.bringToFront(); keypadView.setFocusable(true); //NOT WORKING keypadView.setFocusableInTouchMode(true); //NOT WORKING keypadView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // NOT WORKING Log.d(TRIALTAG,"Inside on click on the keypad"); } }); } public class TrialView extends View{ @Override public boolean onTouchEvent(MotionEvent event){ //IS GETTING CALLED if(event.getAction() == MotionEvent.ACTION_DOWN) { selX = (int)event.getX(); selY = (int)event.getY(); Log.d(TTAG,"Touch X : " + selX + " Touch Y : " + selY); getRect(selX, selY); } return true; } .... } public class CustomKeyPad extends LinearLayout{ @Override public boolean onKeyDown(int keyCode, KeyEvent event){ Log.d(KTAG, "Inside keypad on key down"); //NOT GETTING CALLED switch(keyCode){ case KeyEvent.KEYCODE_1:selectedNumber = 1; isNumberSelected = true; break; default:selectedNumber = 0; isNumberSelected = false; break; } if(isNumberSelected == true){ Log.d(KTAG, "Is selected is true calling update puzzle"); trialActivity.updatePuzzle(selectedNumber); } return true; } EDIT 2 - After placing the setOnTouchListener on my CustomKeypad view class, I found out that clicking on any button within this view, causes the onTouchEvent() of the TrialBoard view class being called, BUT clicking anywhere around the keys, but within the CustomKeypad view actually triggered the onTouchListener in my main activity...I have no idea why this is happening...:(