Skip to content Skip to sidebar Skip to footer

How To Know Which View Is Being Touched

I'm trying to build a Ping Pong game, that the paddle is shown in the bottom of the screen and the bricks is on the top. I use three views, one for the paddle, one for the ball and

Solution 1:

You can achieve this in another way like, just draw two relative layouts blue and yellow same as in your case and make it both relative layout as a clickable : true from the xml and handle the onClick:"onClickHandler". hope this will work for you.

                <RelativeLayout
                    android:id="@+id/rl_click_blue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:background="#ffffff"
                    android:clickable="true"
                    android:onClick="onClickHandler">
                </RelativeLayout> 

                <RelativeLayout
                    android:id="@+id/rl_click_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:background="#ffffff"
                    android:clickable="true"
                    android:onClick="onClickHandler">
                </RelativeLayout> 

               // Handle your functionality here.
               public void onClickHandler(View v)
               {
                   switch (v.getId()) {
                       case R.id.rl_click_blue:
                           break;

                       case R.id.rl_click_yellow:

                          break;
                   }
               }

Solution 2:

Android's OnTouch event already has a View type parameter with which you can identify the view that fired the event. You can find an example here.


Post a Comment for "How To Know Which View Is Being Touched"