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{
@Override
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;
}else
if((event.getAction() == MotionEvent.ACTION_CANCEL)||(event.getAction()==MotionEvent.ACTION_OUTSIDE)){
return false;//TRIED THIS BUT NOT WORKING
}else{
return super.onTouchEvent(event);
}
}
public class CustomKeypad extends LinearLayout {
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.d(KTAG,"Inside onTouch of keypad");
return true;
}else{
return super.onTouchEvent(event);
}
//NOT GETTING CALLED AT ALL
}
}
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);
keypadView.setFocusableInTouchMode(true);
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="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/keypad_background" >
<TableRow >
<Button android:id="@+id/keypad_1"
android:text="@string/key1"
android:padding="2dp"
android:onClick="num1Clicked"/>
<Button android:id="@+id/keypad_2"
android:text="@string/key2"
android:padding="2dp"
android:onClick="num2Clicked"/>
<Button android:id="@+id/keypad_3"
android:text="@string/key3"
android:padding="2dp"
android:onClick="num3Clicked"/>
</TableRow>
<TableRow >
<Button android:id="@+id/keypad_4"
android:text="@string/key4"
android:padding="2dp"
android:onClick="num4Clicked" />
<Button android:id="@+id/keypad_5"
android:text="@string/key5"
android:padding="2dp"
android:onClick="num5Clicked" />
<Button android:id="@+id/keypad_6"
android:text="@string/key6"
android:padding="2dp"
android:onClick="num6Clicked" />
</TableRow>
<TableRow >
<Button android:id="@+id/keypad_7"
android:text="@string/key7"
android:padding="2dp"
android:onClick="num7Clicked" />
<Button android:id="@+id/keypad_8"
android:text="@string/key8"
android:padding="2dp"
android:onClick="num8Clicked" />
<Button android:id="@+id/keypad_9"
android:text="@string/key9"
android:padding="2dp"
android:onClick="num9Clicked" />
</TableRow>
</TableLayout>
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.