Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a parent Activity which contains 2 views. One view is a custom view class, where I am overriding the onDraw method. This view contains an onTouchEvent function, which is checking for user clicks within the view.

Below this view I have another view, which is a view extending LinearLayout, which contains 9 buttons which are numbers to be selected by the user[I am basically trying to build a simple Sudoku game]. Now user clicks on my top view, which is the Sudoku Grid works fine, but it is calling the onTouchEvent of the top view, even when I click on the buttons within the bottom view keypad.

I have tried everything, from setFocusable(true), in the XML, in the constructor, from the parent activity. I even placed a onTouchListener on the keypad view. This listener is getting called when I click anywhere within the keypad view, except for on the buttons???

How can I detect a keyPress on the bottom view??

The bits of code

public class TrialBoard extends View{
public boolean onTouchEvent(MotionEvent event){
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;
if((event.getAction() == MotionEvent.ACTION_CANCEL)||(event.getAction()==MotionEvent.ACTION_OUTSIDE)){
 return super.onTouchEvent(event);


public class CustomKeypad extends LinearLayout {

public boolean onTouchEvent(MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN)
        Log.d(KTAG,"Inside onTouch of keypad");
        return true;
        return super.onTouchEvent(event);

public class TrialActivity extends Activity {

public void onCreate(Bundle savedInstanceState){
    Log.d(TRIALTAG,"Inside on create");
    keypadView = findViewById(; 
    selNumber = 0;
    keypadView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event){
            Log.d(TRIALTAG,"Inside keypad view touched listener event");
            return true;

EDIT 2: My XML for keypad:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:background="@drawable/keypad_background" >

<TableRow >
   <Button android:id="@+id/keypad_1"
   <Button android:id="@+id/keypad_2"
   <Button android:id="@+id/keypad_3"
<TableRow >
    <Button android:id="@+id/keypad_4"
         android:onClick="num4Clicked" />
    <Button android:id="@+id/keypad_5"
         android:onClick="num5Clicked" />
    <Button android:id="@+id/keypad_6"
         android:onClick="num6Clicked" />
<TableRow >
    <Button android:id="@+id/keypad_7"
         android:onClick="num7Clicked" />
    <Button android:id="@+id/keypad_8"
         android:onClick="num8Clicked" />
    <Button android:id="@+id/keypad_9"
         android:onClick="num9Clicked" />

I added the onClick later, coz I realized too late that this was a easier and saner way to get a button click event. I guess in a way I do deserve a flag down coz I should have remembered this, but I am still puzzled as to why it would not pick up the button click on the onTouchEvent.

share|improve this question
Thanks for the response @Evan.but your comment is gone now..but yes that is precisely what I want to do. I want each child view to handle their own onTouchEvent functions. How can I differentiate which View was touched, when onTouchEvent in Parent is called? – monologish Apr 17 '13 at 14:21
why did I get flagged down?? I'm still new here, so if you can let me know what I did wrong here I would appreciate it.. – monologish Apr 17 '13 at 18:07
Can you post your XML? I think I know what's going on – Jay Soyer Apr 21 '13 at 12:44
@JaySoyer Have updated my XML. I added the clicks later, which is correctly picking up click events. So would I be correct to assume that the onTouchEvent does not pick up these events, coz the onClick of buttons are the only way to handle these?? – monologish Apr 22 '13 at 14:23
Almost there. What does trial_activity look like? Is your custom view added dynamically? I'm trying to see if your custom view's bounds are simply over lapping the linear layout. Also, when you say top/bottom. You don't mean parent/child right? – Jay Soyer Apr 22 '13 at 15:33
up vote 1 down vote accepted

It does not work because the buttons are Views too, just as LinearLayout and since they are on top they need their own "onTouchEvent" listener, which is onClick listener in their case. LinearLayout does not detect the touch on its children views because the children overtake the touch events for themselves.

But did you try to touch around the button? Then the LinearLayout's onTouchEvent may work.

share|improve this answer
You are correct, the onClick for the individual buttons works, but not the onTouch in the LinearLayout. onTouch on LinearLayout is triggered when clicking around the buttons though. Would it be correct to accept this as the answer, coz this is the solution that works in this case. – monologish Apr 26 '13 at 6:18
Yes, it explains exactly what happens in this case and how to do it correctly. – Lumis Apr 26 '13 at 8:27

I believe the problem is that your custom view is somehow overlapping the linearlayout. An easy test is to add the following into your Drawable resource folder.


<solid android:color="@android:color/background_light" />

    android:color="@android:color/black" />

Then for your custom view, set it's background image to the border.xml resource. That background will draw a black outline around the perimeter of your View. You should also comment out the onDraw() method for this test.

share|improve this answer
I tried this, and the custom view is not overlapping the keypad view. Also the keypad view is included after the custom view, so I believe it is above the custom view, or so I have read. Thanks a lot for the answers though :) – monologish Apr 26 '13 at 6:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.