how to disable android ImageButton from javascript - javascript

i have a hybrid app. I used web-view to make a android hybrid app. I also have a native menubar above the webview. My question is, How can i disable the native menu from my web application? Is it possible? If yes please share your thoughts on this

Check the answer of this question, as they provide snippet code to call Java method from javascript in Android, this will help you if you create a Java method that will disable your ImageButton from a Javascript
Call Java function from JavaScript over Android WebView

You will need to find the user-agent click here to see how to check useragent then u can write a simple javascript function on page load to disable your header

Try to set webviewclient to your webview and override onPageFinished like this. In my project I tried to hide google and facebook buttons on fitbit page.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!TextUtils.isEmpty(url) && url.contains("fitbit")) {
view.loadUrl("javascript: setTimeout(function () { $('.external-choices,.or').hide();} , 1000) ");//JS to hide the fitbit login from G+ and FB
}
}
}

Related

Is there a way to get back a result from a PWA I develop to Android using a startActivityForResult from the native app?

I am trying to use a PWA I develop with HTML and JavaScript to process data and send it back to the Android app.
The Android should open the PWA after the user clicks a button for example, I already did this by starting an activity with an intent using the page URL:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://examplePWA.com/"));
Log.d(TAG, "onClick() called with: view = [" + browserIntent.getDataString() + "]");
startActivity(browserIntent);
What I want now is to get a response intent from the PWA to a startActivityForResult (browserIntent, 123);
sent from Android for example.
How can I do this from the PWA side, is it possible to send an Android Intent from a PWA using JavaScript or any other way?
If I got you right, for the purpose of starting an activity from within the javascript env, you could employ the #JavascriptInterface which will allow you to invoke Java methods inside your javascript code (if you use WebView).
class WebBridge {
#JavascriptInterface
public void openBrowser(String href) {
// .. start your activity here
}
}
// Somewhere where you initialize your WebView
webView.addJavascriptInterface(new WebBridge(), "AndroidWebBridge");
// Inside your javascript app code
window.AndroidWebBridge.openBrowser("https://google.com");
The code below illustrates the overall concept, not a copy-paste solution. Hope that helps your.

How can Android code interface with Javascript code in a WebView?

I know you can take an instance of a webview and call addJavascriptInterface(new WebAppInterface(context), "nameofinterface") to allow javascript to send data to Android Java code. But how do I go the other way? I want to tell javascript to play a video at a specific point in time when I hit the back button on my android device. Any ideas?
webView.loadUrl("javascript:function()");
You can put it here:
#Override
public void onBackPressed() {
// ...
}

window.close() not working in android webview?

Android 4.1
Eclipse
Testing on device
I am trying to open a html/javascript webpage, hosted on my localhost for now, in my android application through a webview.
The page is suppose to just close after 2 sec through the below code
<script type="text/javascript">
alert('hello');
setInterval(function(){window.close();},2000);
</script>
I am able to get the hello alert by using WebChromeClient. But the window.close is still not working. Any help would be appreciated.
PS: Here is how i am loading the URL:
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("http://192.168.1.137/abc.html");
webView.setWebChromeClient(new WebChromeClient());
Try this :
WebChromeClient webClient = new WebChromeClient(){
public void onCloseWindow(Window w){
super.onCloseWindow(w);
Log.d(TAG, "Window close");
}
};
Description :
public void onCloseWindow (WebView window)
Notify the host application to close the given WebView and remove it from the view system if necessary. At this point, WebCore has stopped any loading in this window and has removed any cross-scripting ability in javascript.
It's not possible on android version > KITKAT. But I have find solution. If android build version > KITKAT, I have detected message from js on server side in method onConsoleMessage of WebChromeClient class http://developer.android.com/reference/android/webkit/WebChromeClient.html.
It works for me.

How to set Javascript properties in Android webview

I am building an Android application that loads a web application in to a web view. When a user visits the web application in a browser a pop up asking if they want to bookmark the page appears.
For the native app I want remove this.
For iPhone I was able to do this:
[webView stringByEvaluatingJavaScriptFromString:#"AppConfig.showBookmarkPrompt=false;"];
For Android I tried this:
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(function(){"+
"AppConfig.showBookmarkPrompt=false;})()");
}
The Android version doesn't work. I presume that the javascript is being run after the bookmark popup has been shown. I don't really know why the iPhone version does works and not the Android.
There are many other ways I can achieve my goals but I am wondering if anybody can explain why the iPhone method works and if there is something similar in Android. I have been reading about it through Google searches but it is still not entirely clear to me.
Thanks for any help I get.
EDIT
The problem was for Android I was passing a function in JavaScript where I should have just set the property like this: view.loadUrl("javascript:AppConfig.showBookmarkPrompt=false;");
There must be some difference in the way it is executed that I don't quite understand.
put this on onCreate() of Activity..
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
then call your code...
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:AppConfig.showBookmarkPrompt=false");
}
For more info look at here.

javascript not working in android

I have a webview in my program.
I loaded a string into this webview that contains javascript function. When i loaded it in the emulator it's not working
i.e if I write a simple alert the webView won't display the alert.
I have enabled the javascript. But then also its not working.
what may be the reason?
Please help
Thankyou
Alerts will not normally work in a WebView. You will need to write the code to do that yourself. You can easily do that by implementing your own version of WebChromeClient. In otherwords:
class ChromeClient extends WebChromeClient {
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
...
mWebView.setWebChromeClient(new ChromeClient());
Another thing I would recommend is implementing the onConsoleMessage as well. This way you can just use "console.log" in your JavaScript and have it directed to Toast or the Android Log.

Categories