I have a WebView that shows the content from a HTML file with a SVG object and inside it some clickable objects with a onclick() function calling the Android interface with java.performClick().
Here's a code sample:
WebView wvMapa = (WebView) findViewById(R.id.wvMapa);
wvMapa.getSettings().setBuiltInZoomControls(true);
wvMapa.getSettings().setSupportZoom(true);
wvMapa.loadUrl("file:///" + file.getAbsolutePath());
WebSettings wsMapa = wvMapa.getSettings();
wsMapa.setJavaScriptEnabled(true);
wvMapa.addJavascriptInterface(new Object(){
public void performClick(String id){
/*MY ACTION*/
}
}, "java");
It works great on the first run, but when I click Home button and after Resume or open another Intent and go back, it doesn't work anymore. Looks like the JavascriptInterface is unattached everytime I leave the screen.
What's missing?
Related
I want to make an app where the user can get random youtube videos in a webview by clicking a button.
I already can show a video in my webview with the url but I want to get any random video from youtube by clicking my button.
In my xml file I have a webview and a button
´´´
MainActivity.java
´´´
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = "https://www.youtube.com/watch?v=1uwvxTT5V5M";
String dataUrl =
"<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView showYoutubeVideo = (WebView) findViewById(R.id.zufallvideo);
showYoutubeVideo.setWebViewClient(new WebViewClient(){
public boolean shouldoverloadUrlLoading(WebView view, String url) {
return false;
}
});
showYoutubeVideo.getSettings().setJavaScriptEnabled(true);
showYoutubeVideo.loadData(dataUrl,"text/html", "utf-8");
}
}
TL;DR: it's complicated.
Until Youtube exposes an endpoint in their API to do this, you're gonna need to find a workaround.
Some would suggest to query a search with "IMG" followed by a random integer (ex. "IMG 22930"). The end result is quite surprising, since tons of videos are uploaded without renaming.
Here's an example of this workaround implementation in python: https://github.com/0x01h/random-youtube-video-generator
Context: I'm trying to link an authenticated user in my android app to a Facebook fanpage PSID using the "Send to Messenger" Plugin to be able to send messages to that specific user on certain events that responds to the user actions in other systems.
I figured the best way to achieve this (since said plugin only works with the Facebook Javascript SDK) was to open a WebView Activity in my app, loading a website with the plugin button.
The website includes the following to load the SDK and the button:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<<Here I put my FB App ID>>',
autoLogAppEvents : true,
xfbml : true,
version : 'v2.12'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-send-to-messenger"
messenger_app_id="<<Same FB App ID>>"
page_id="<<FB fanpage ID>>"
data-ref="<<user identification>>"
color="blue"
size="standard">
</div>
Here's the onCreate method of the Webview Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
#Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("WEBVIEW_CONSOLE", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId());
return super.onConsoleMessage(consoleMessage);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(getString(R.string.authenticated_webview_url_example), headers);
Expectation: The facebook Send to Messenger Button should load in the WebView, allowing the user to register a PSID on my Facebook fanpage/App.
Problem: When I load the website on the Android WebView, the button is not shown.
Details: Using Chrome Remote Debugging, I was able to determine the Facebook SDK was working and almost all the elements of the buttons were loaded, except for the content of the iframe generated.
Tested the Facebook jssdk by using a Like button, which worked and was shown.
No errors in website console or logcat.
Why is WebView not showing that particular button?
Testing website: https://www.vsdesigns.cl/debug/login.php
Edit:
Here is an image of the testing website from a desktop browser. It shows the button.
Here is another image with the testing website being loaded by the WebView. It's actually a representation of the chrome remote debugger, but it's the same as what my smartphone screen shows. It doesn't show the button.
Thanks in advance.
I figured out the Facebook Cookies weren't loading at all, and Facebook doesn't render the plugin if the App is in development and the user is not previously logged in, so I implemented the following code to accept Third party cookies. (Only works on SDK version 21 Lollipop and up)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setAcceptThirdPartyCookies(mWebview,true);
}
mWebview would be the variable storing the actual WebView.
I'm trying to call JS functions from Android native code, but they don't seem to be working. I've tried many solutions but to no solution. Here is my code:
public class MainActivity extends Activity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyCustomWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("http://google.com");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:alert('shadow');");
Toast.makeText(getApplicationContext(), "DONE", Toast.LENGTH_LONG).show();
}
}
}
Please help.
Thanks
You can't do alert() with Android's WebView. If you want to show alert dialogs, you need to handle it in your Activity code or use a WebChromeClient. Per the docs:
Creating and setting a WebChromeClient subclass. This class is called
when something that might impact a browser UI happens, for instance,
progress updates and JavaScript alerts are sent here (see Debugging
Tasks).
See: https://developer.android.com/reference/android/webkit/WebView.html
With a WebView you can still write a JavaScript function that sets the value of a textbox to some string. If you need specific code to help you with this, let me know. If you've gotten this far though, you probably can handle it on your own is my guess.
Edit 1
First create a method in JavaScript called sendValueToAndroid()
myWebView.loadUrl("javascript:sendValueToAndroid()");
In the JavaScript method, call an exposed method in your Android code.
function sendValueToAndroid()
{
val divValue = ...
Android.sendValueToAndroid(divValue);
}
Expose a method in the Android app in any object of your choosing.
#JavascriptInterface
public String sendValueToAndroid(String val)
{
//do something in your app
}
Basically, what you're doing is telling the WebView to invoke a JavaScript method which invokes a callback method in your own app.
I found that this question has been asked before How to resolve overlapping the content of the webview here, but it was closed for some reason. But here is what happens. I have created a webview, applied transparent background, but when i try to load something, and reload something again, the older content will still remain on the webview and the newer one will overlap it. Here is a picture that explains it.
So i am creating a jokes application, and i have added webview. Then i added transparency and the random joke button reloads the webpage so that the webview can get another joke. But it just overlapps instead. Any solutions? Here is my piece of code
Button random = (Button) findViewById(R.id.reload);
final WebView web = (WebView) findViewById(R.id.webView);
web.setBackgroundColor(Color.TRANSPARENT);
web.setWebViewClient(new WebViewClient());
String selected = (String) getIntent().getCharSequenceExtra("selected");
final String url = "http://someurl.com/?s=dirty&x=" + selected;
web.loadUrl(url);
random.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
web.loadUrl(url);
}
});
Any help would be really appreciated :)
Try before doing web.loadUrl(url); doing web.clearView();
See docs.
In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".