How to disable IOnTouchListener

Shay Wilner 1,746 Reputation points
2022-01-11T20:26:59.727+00:00

Hello

I implemented a class GameAreaTouchListener to detect if i touch a relative layout called gamearea;

class GameAreaTouchListener:Java.Lang.Object ,RelativeLayout.IOnTouchListener
        {
            public GameAreaTouchListener(RelativeLayout rl )
            {
                Gamearea = rl;
             }
            private RelativeLayout Gamearea { get; }
             public bool OnTouch(View v, MotionEvent e)

            {

                switch (e.ActionMasked)
                {
                    case MotionEventActions.Up:
                               //blablabla   

                        }
                            break;
                }
                return true;
            }
        }

i have two buttons

private void Button1_Click(object sender, System.EventArgs e)
//  i  create an instance of class  GameAreaTouchListener
        {
            gat = new GameAreaTouchListener(gamearea);
           gamearea.SetOnTouchListener(gat);

        }


 private void Button2_Click(object sender, System.EventArgs e)
            {


            }

When i click Button2 i would like to disable OnTouch(View v, MotionEvent e) of GameAreaTouchListener ?

Thanks

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2022-01-12T01:57:27.04+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Android.Views.View class doesn't provide the method to remove the onTouchListener. A workaround is to set another touchListener to the view instead and do nothing in the OnTouch event.

    Here is the sample code, you could refer to it.

       private void Button2_Click(object sender, System.EventArgs e)  
       {  
           gamearea.SetOnTouchListener(new DoNothing_TouchListener());  
       }  
         
         
       class DoNothing_TouchListener : Java.Lang.Object, IOnTouchListener  
       {  
           public bool OnTouch(View v, MotionEvent e)  
           {  
               return false;  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.