Android Question Is there a way to simulate a click?

Rorry

Member
Licensed User
Longtime User
Hi,
how can i simulate a click (touch event) on my own application at a specific X Y point on the screen.

Thanks.
 

Informatix

Expert
Licensed User
Longtime User
I tried what Informatix suggested, but sadly no succes. This code does not work:
B4X:
Dim MEv As Object = GD.CreateMotionEvent(DateTime.Now, DateTime.Now, GD.ACTION_DOWN, 40%x, 90%y)
GD.PassTouchEventTo(MEv, WebView1)
Dim MEv As Object = GD.CreateMotionEvent(DateTime.Now, DateTime.Now, GD.ACTION_MOVE, 10%x, 10%y)
GD.PassTouchEventTo(MEv, WebView1)
Dim MEv As Object = GD.CreateMotionEvent(DateTime.Now, DateTime.Now, GD.ACTION_MOVE, 20%x, 20%y)
GD.PassTouchEventTo(MEv, WebView1)
Dim MEv As Object = GD.CreateMotionEvent(DateTime.Now, DateTime.Now, GD.ACTION_UP, 20%x, 20%y)
GD.PassTouchEventTo(MEv, WebView1)
You should let some time between the events (instead of putting DateTime.Now everywhere) and if it's a horizontal scroll you should keep the same horizontal position. But I don't say that will work as I have no idea of how a WebView behaves and what it expects. I never used one. Anyway, using GD for scrolling a view, is a bad idea IMHO.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
There is a javascript function scrollIntoView().

This will adjust the webpage until the specified HTML element is within the viewport.
It's supported by most modern browsers but the android WebView seems nots to support it :(.
That's a shame, if it was supported you could possibly scroll the element you want to click into the viewport, give it the focus in the WebView and then simulate a 'click' or 'select' event in the webpage.
The click or select event would (again possibly!) cause the focussed HTML element to act as though it had been clicked.

I wonder if, even though scrollIntoView is unlikely to work, you could execute some javascript to focus the webpage on the button to click and then simulate a click or select.
You'd not have to worry about simulating a touch event and working out whereabouts you want to simulate that touch event.

Here's some links that might help:

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Thank you Martin for this detailed information. Could you maybe do me a big favour and investigate why strange things happen with my code (that is given in thread #22 above) when the full screen button in the bottom right corner of the video player is touched? In the regular Chrome Browser there is no problem with http://www.npo.nl/kassa/10-05-2014/VARA_101341883", but in my code in Post #22 things go wrong: The video freezes and full screen never happens. Thank you in advance!

I'm not sure what the problem is.
On my Moto G running Kitkat the webpage loads and i start the video - it plays ok.
Clicking the full screen button does nothing, i see the entire video window (the shockwave player) highlights as selected but it doesn't change to full screen.

I did notice that the video player is actually playing in an iframe element within the loaded webpage.
Perhaps the video player in an iframe cannot expand larger than it's containing iframe?

The iframe also gives you another possibility, maybe what you were after in the first place.
Look at this updated PageFinished Sub:

B4X:
Sub WebView1_PageFinished(Url As String)
	Log("WebView1_PageFinished Url="&Url)
	Select Url
		Case "http://www.gemistvoornmt.nl/a.aspx/453063900/0"
			Dim Javascript As String
		    '	Javascript="B4A.CallSub('ProcessHTML', false, document.getElementById('playerID').src)"
		    Javascript="window.location.href=document.getElementById('playerID').src"
		  	WebViewExtras1.executeJavascript(WebView1, Javascript)
	End Select
End Sub

The original webpage loads, then the javascript gets the url of the content being played in the iframe and loads the iframe content as the webview content.
The video player now displays full screen - once the video player has loaded you might then be able to execute javascript to start playback.
https://www.google.co.uk/search?q=javascript+control+shockwave+video&ie=UTF-8&oe=UTF-8

Martin.
 

Attachments

  • Uitzend1-20140518.zip
    6.9 KB · Views: 367
Upvote 0

warwound

Expert
Licensed User
Longtime User
The video is played using a Shockwave video player component, so you're asking if there's a way to listen for the Shockwave video player entering full screen mode.
A Google search shows nothing of use: https://www.google.co.uk/search?q=webview+javascript+shockwave+video+player&ie=UTF-8&oe=UTF-8.
In fact there are many various branded or third party Shockwave video player components, your webpage loads a Shockwave video player from this url: http://npoplayer.omroep.nl/media/jwplayer/6.5.3609/jwplayer.flash.swf
The JWPlayer website might have developer information which shows if javascript can interact with the JWPlayer...
The documentation does state:
autostart
Automatically start playing the video on page load. Can be true or false (default). Autostart does not work on mobile devices (iOS and Android). See Autostarting Videos for more info.
Under that, under controls it mentions that javascript can be used to control the player.
Finally there's some examples of using javascript to control the JWPlayer on this page: http://support.jwplayer.com/customer/portal/articles/1439411-example-a-chromeless-player.

To directly use javascript to control the video player you need a reference to it, this is the HTML fragment that displays the video player:

PHP:
<object width="100%" height="100%" type="application/x-shockwave-flash" data="http://npoplayer.omroep.nl/media/jwplayer/6.5.3609/jwplayer.flash.swf" bgcolor="#000000" id="player" name="player" tabindex="0">
  <param name="allowfullscreen" value="true">
  <param name="allowscriptaccess" value="always">
  <param name="seamlesstabbing" value="true">
  <param name="wmode" value="opaque">
</object>

The object element has an id attribute whose value is player.
Does that mean we have a javascript reference to the JWPlayer and the reference is the javascript 'variable' player?
Or is player just an HTML element and not the actual video player that we want to control?

You could wait for the webpage to load as in my other post, load the iframe source and wait for that to load - you now have just the video player loaded in the WebView and not the entire original webpage - and now execute javascript such as:

B4X:
player.play();  //  does this start playback of the video?

player.resize(320, 180);  //  does this resize the video player?

Can't see a javascript method to toggle full screen though, the javascript reference can be found here: http://support.jwplayer.com/customer/portal/topics/564475-javascript-api/articles
Might be worth your while having a read of it and googling for more info on the JWPlayer.
I see there were recently problems with full screen due to an Adobe Flash update: http://support.jwplayer.com/customer/portal/questions/5405357-toggle-fullscreen-not-working-

Martin.
 
Upvote 0
Top