0

Xamarin forums question link: https://forums.xamarin.com/discussion/60321/disabling-multitouch-across-the-appwide-in-android-xamarin#latest

Is there a way to disable multi-touch App wide in Xamarin Android. I currently have a PCL, project. With shared code implemented using Xamarin forms UI.

I just want to prevent users from clicking more than 1 button in Android.

All our buttons are ICommand based and called from the viewModel. They are all async Tasks (we wanted to make them non-blocking and run in the background thread). --> This approach works fine in iOS, just want to disable it App wide in Android.

I tried the following in MainActivity.cs

public override bool OnTouchEvent(MotionEvent e)
{
    if(e.PointerCount > 1)
    {

     return false;
    }
    else
    {
        return base.OnTouchEvent(e);
    }

}

But it has not made any difference. Any help would be appreciated

public override void OnUserInteraction()
    {
        base.OnUserInteraction();
    }

works but does not give me the number of touch events

wsr007
  • 61
  • 4

1 Answers1

0

Apply an application theme that disables multi-touch (derived from this answer).

Under Resources/Values add a file named Styles.xml with the following code:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">    
        <item name="android:windowEnableSplitTouch">false</item>
        <item name="android:splitMotionEvents">false</item>
    </style>
</resources>

android:windowEnableSplitTouch indicates if touches can be split across other windows that also support split touch.

android:splitMotionEvents indicates if a ViewGroup should split MotionEvents to separate child views during touch event dispatch.

Applying these at the application theme level will forced single touch for all views in your application.

And then in your applications manifest apply the style to the android:theme attribute on the application:

enter image description here

enter image description here

Sample manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        android:versionCode="1" 
        android:versionName="1.0" 
        package="com.companyname.testapp">
    <uses-sdk android:minSdkVersion="11" />
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/icon" 
        android:label="@string/app_name" 
        android:theme="@style/MyAppTheme">
    </application>
</manifest>

This will disable multi-touch throughout the application.

Community
  • 1
  • 1
matthewrdev
  • 11,930
  • 5
  • 52
  • 64
  • @matthewdev. Thanks for the answer. I have come up with a solution to the this issue. It is a different approach. Here goes: 1) When I click 2 buttons at the same time (the page pushasync() in both get fired) 2) However Device.BeginInvokeOnMainthread only renders one page at a time (sequential by nature). So i set a flag to false during both button presses and only set it to true during onAppearing of either page. For good measure i used Device.StartTimer(new Timespan(0,0,60). To have flag set to true occur after a minute. This seems to have stopped the multipage pushes on multiple button tap – wsr007 Feb 04 '16 at 14:45
  • My solution is posted on this thread on Xamarin forums:https://forums.xamarin.com/discussion/comment/178741#Comment_178741 – wsr007 Feb 04 '16 at 15:00
  • @wsr007 Glad to see you found a solution! – matthewrdev Feb 04 '16 at 21:27