3

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...:(

3
  • Post some code, that will help us to help you. Commented Apr 16, 2013 at 14:12
  • You are using onkeydown in some other class , copy that code in to your activity Commented Apr 16, 2013 at 15:50
  • @Arju - Tried it with onKeyDown in the Activity, still does not get called..:( Commented Apr 16, 2013 at 15:58

3 Answers 3

1

Try override the OnKeyDown Method in your custom Keypad Class which extends LinearLayout

public class YourCustomKeypadLinearLayout extends LinearLayout { public YourCustomKeypadLinearLayout(Context context) { super(context); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if( keyCode == YourKeyCode ){ //do sth } else{ return super.onKeyDown(keyCode, event); } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I am already doing this, sorry I didn't post the code earlier. This function is not getting called at all. I think @anzaidemirzoi may be right, am trying to find out how to differentiate between clicks within a single onTouchEvent class now..
strange, worked for me last time. you could try override the onTouchEvent-Function as well. Could be your LinearLayout hasn't the focus and the event is catched by some other UI-Element (test this with overriding the onFocusChanged).. and next time post some code what you already tried ;)
0

if you implement onTouchEvent it overrides other user interaction interfaces. so if you implement it you need to write your own events.

1 Comment

Thanks for the response, but can you tell me what you mean by write my own events? I'm still new to Android programming, so any links or information about this would be highly appreciated.
0

The touch event might be consumed, you should try returning false, or calling the superclass method, when the touch event is not handled.

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; } else { return super.onTouchEvent(event); } 

1 Comment

I tried your suggestion, with both onKeyDown within my CustomKeypad, as well as within TrialActivity classes, but neither scenario works. The function is not getting called. Only onTouchEvent is called every time.Is there a way for me to tell which View called the onTouchEvent?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.