0

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 another one that is the main view that paints the two views (ball and paddle):

public void draw(Canvas canvas){
    BallView.draw(canvas);
    PaddleView.draw(canvas);
}

What I want to know is if I can identify which view is being touched exactly, I try to do it with onTouchEvent in the main Activity:

   @Override
    public boolean onTouchEvent(MotionEvent event){
        Log.d("touched", "touched")
        return true;
    }

But, the problem is that no matter where I touch on the screen, the paddle is moving, and I want, that only when I touch the Paddle he will move. SO, how can I do this, if it is possible? Thanks.

Ofir A.
  • 3,112
  • 11
  • 57
  • 83
  • Give both views a different id.... private OnTouchListener tListener1 = new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) than simply do if(v.getId==R.id.blue) {} else if(v.getId==R.id.yellow){} I hope this will work. – aalionline Feb 16 '12 at 11:18
  • @pakshaheen can I set the Id from the java file? because I don't have any XML file. – Ofir A. Feb 16 '12 at 11:27
  • yes, you can do e.g. ScrollView view=new ScrollView(this); view.setId(2); – aalionline Feb 16 '12 at 12:00
  • @pakshaheen Thanks, but the problem is that the OnTouchListener is never been called... – Ofir A. Feb 16 '12 at 12:02

2 Answers2

2

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;
                   }
               }
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
0

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.

Narcís Calvet
  • 7,304
  • 5
  • 27
  • 47