I have a simple game developed in PHP. I have loaded the remote site in Android WebView. I want to find out that if user clicks on a FREE life button which is on my remote PHP site, I want to start a reward video on my Android app.
But how can I know whether the user clicked on the FREE life button in my WebView and start the video instantly in my android app?
There is an Android mechanism that alows you to run Android function from javascript:
<input class="button" type="button" value="FREE life" onclick="startRewardVideo('some parameters can be passed to Android from here')">
<script type="text/javascript">
function startRewardVideo(paramFromJS) {
Android.startRewardVideoAndroidFunction(paramFromJS);
}
</script>
now you need class that knows what to do with your javascript:
public class MyJavaScriptInterface {
#JavascriptInterface // this annotation is importatn
public void startRewardVideoAndroidFunction(String paramFromJS) {
//here you need to start showing reward movie
//because this function will be called after webView button click.
}
}
last step is to connect webView with your javascript interface:
webView.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
and of course don't forget to enable javascript for your webView:
webView.getSettings().setJavaScriptEnabled(true);
Hope it helps :) Ask if you have any questions on this.
Here you have full tutorial
Related
I have a testing website where PayUMoney payment processing in test mode is working perfect.
I used the same url in webview and also enabled javascript, but the payment is processing infinitely(Loading forever).
Please help me figure out this. Thanks!
public class MainActivity extends AppCompatActivity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("https://gatewaypayment.000webhostapp.com");
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webview.canGoBack()) {
webview.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
I faced a similar situation but finally figured out that the reason behind the long loading is due to the test details getting updated frequently, or the test environment that PayUMoney provides itself is down. Also make sure of the following:
To test the Bolt Checkout in the test mode, you need to change the JavaScript libraries used as-well as use Test Mode key and salt for hash generation.
So while integrating in Test Mode, include the script as shown below in the head section of your payment request page.
<script id="bolt" src="https://sboxcheckout-static.citruspay.com/bolt/run/bolt.min.js" bolt-
color="<color-code>"
bolt-logo="<image path>">
</script>
Once your integration is done and tested on the sandbox, replace the sandbox JavaScript library with the production library provided by PayU.
<script id="bolt" src="https://checkout-static.citruspay.com/bolt/run/bolt.min.js" bolt-color="<color-
code>"
bolt-logo="<image path>">
</script>
I'm trying to track what links my users click in the browser and failing miserably:
My code:
browser = new XWalkView(getMainActivity());
browser.setResourceClient(new XWalkResourceClient(browser)
{
#Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url)
{
log.i("juhu 1", url);
return false;
}
});
This only calls the callback for URLs that I give it (browser.load()), but not for URLs that user then clicks on the rendered page. What's worse, it's not consistent: sometimes the callback gets called, sometimes not.
Here's an example that fails:
<html>
<body>
<p>my link 1</p>
<p>my link 2</p>
</body>
</html>
I tried this with XWalk 15.44.384.13 (latest) and 14.43.343.24 (a couple revisions back), both with no success.
I looked all over the place for similar methods, but neither resource client nor ui client seem to provide something that would work.
This looks fixed as of at least XWalk 16.45.421.19 with the call being made as expected.
I have a WebView contains some html code, this html code is a code that displays five buttons created through it, I want to listen to the onClick or onTouch action of these buttons so that when the user clicks on it some action happens, all I want to know is how to assign onClick for these views which is created by html, and for sure the onClick process can be added to buttons through the html code itself so it will OK also if there is a way to assign an onClick regarding to html code.
You need to write JavaScript for HTML button click listeners.
<button type="button" id="exit" onclick="exitButtonClick();">Exit</button>
<script language="javascript">
function exitButtonClick()
{
// Do Something
}
</script>
If you want to call native java code from HTML's buttons then you need to add JavaScriptInterface to your WebView. For details please check below link
http://developer.android.com/guide/webapps/webview.html
You can't add listener for html's buttons in android code. Instead, you can use Javascript Interface with Webview.
public final class MyJSInterface
{
public void button1function(){
//do something
}
}
Write all functions in this Interface.
Add that JSInterface to your webview.
myWebView.addJavascriptInterface(new MyJSInterface(), "MyJSInterface"); //name you want that will be used in javascript file.
Now in your html's javascript, you can call these functions using JavaScriptInterface name:
function method1() {
MyJSInterface.button1function();
}
Hope this helps.
I have an android application that loads a webview from a server. I do not have the server code so I cannot change anything in Javascript. I want to figure out when a button is being clicked in a webview and what is the label in the button. I do not know the Id, I just want to get the label.
I tried searching for this but could not find an answer. I found solutions where you can work in the javascript but in my case I cannot.
This suggestion may help to find useful information that could lead to determination of your button label. Override shouldOverrideUrlLoading(), shouldInterceptRequest() and/or onLoadResource() for the WebViewClient so you can get at the URL of any redirects.
Example:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Try to learn something useful from the 'url' here.
// Continue as normal, loading the 'url' within this WebView.
view.loadUrl(url);
return false; // Allow the WebView to handle the request.
}
// Optional: Add similar for "shouldInterceptRequest()" and/or "onLoadResource()".
});
Note: Overriding shouldOverrideUrlLoading() as above is the standard way to keep redirects within the same WebView rather than redirecting to the default browser application.
You might really want to check this page:
Building Web Apps in WebView (Google API Guides)
Specifically, it seems that addJavascriptInterface might be what you are looking for:
addJavascriptInterface(Object object, String name)
It allows you to execute your Java code from javascript and, paired with the ability to insert code in a page, it's an incredibly powerful tool for granting you a high level of coupling between your Activity and your page.
I think that at this point you will already know what to do, but I'll sketch a possible course of action anyway:
create a javascript interface with the callbacks you want executed in your activity when a button is pressed
as soon as your page loads, install the code to call your javascript interface in each button (or link) by injection
Hope this helps
I am using Phonegap + Jquery Mobile for a Web App. I have a simple registration form. However, when i type any character, the page jumps and flickers Wildly. How do i overcome this problem on Android?
i have tried most of the solutions provided at :
https://github.com/desandro/isotope/issues/251
http://blogs.bytecode.com.au/glen/2011/07/14/eliminating-annoying-flicker-transitions-with.html and other pages. None of the solutions worked for me.
Or Is it possible to call an android page only for registration? Any help much appreciated. Thank you.
It is possible to launch the Android code from your phonegap html page
MyWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "MyHandler");
public class MyJavaScriptInterface
{
Activity parentActivity;
MyJavaScriptInterface(Activity activity){
parentActivity = activity;
}
public void processData(String parameter){
/* your processing logic */
/* you can launch your activity here. which contains registration form logic*/
}
in your html page
<a href="#" data-transition="slide" onClick="window.MyHandler.processData("+value_to_pase+")">
Change this with your page code (html Page)