Forum Xamarin Xamarin.Android

OnTouch Listener on my WebView

Xammy10Xammy10 Member ✭✭✭

Im creating an app that in the WebView there's a OnTouchListener and Timer(10 seconds). That if someone is touching the screen, the Timer will go back to 1. As long as someone is using the app or touching the diplay that timer will loop. The problem is, I can't do anything in the WebView, I can't touch the content, i can't manipulate the website. The Question is, how can I make the webview touchable again?

HERE IS MY CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;

namespace TouchEventSample
{
[Activity(Label = "openActivity")]
public class openActivity : Activity, View.IOnTouchListener
{
private LinearLayout cancelBtn;
private TextView txtCountDown;
private int count = 1;
Timer timer;

    public bool OnTouch(View v, MotionEvent e)
    {
        switch (e.Action) {
            case MotionEventActions.Move:
                count = 1;
                txtCountDown.Text = "" + count;
                base.OnResume();
                break;
            }
        return true;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        float X;
        WebView mWebView;
        LinearLayout mLinear;
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.openActivity);
        cancelBtn = FindViewById<LinearLayout>(Resource.Id.cancel_btn);
        txtCountDown = FindViewById<TextView>(Resource.Id.countDownView);


        //Timer(20 Seconds)
        timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += Timer_Elapsed;
        timer.Start();

        //Web View
        mWebView = FindViewById<WebView>(Resource.Id.web_view);
        mWebView.SetWebViewClient(new ExtendWebViewClient());
        mWebView.Settings.JavaScriptEnabled = true;
        mWebView.LoadUrl("https://www.youtube.com/watch?v=6Jx7ZNZL9dk&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l");
        //mWebView.Touch += MWebView_Touch;
        mLinear = FindViewById<LinearLayout>(Resource .Id.cancel_btn);
        mWebView.SetOnTouchListener(this);
    }
  //  private void MWebView_Touch(object sender, View.TouchEventArgs e)
   // {
  //      count = 1;
  //      txtCountDown.Text = "" + count;
  //      base.OnResume();
 //   }


    //count = 1;
    //txtCountDown.Text = "" + count;
    //base.OnResume();
    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (count<10)
        {
            count++;
            RunOnUiThread(() =>
            {
                txtCountDown.Text = "" + count;
            });
            if (count == 10)
            {
                StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            }
        }
    }
}

internal class ExtendWebViewClient : WebViewClient
{
}

}

~Thanks in Advance :smile:

Sign In or Register to comment.