Webview Development
Following section is for Android webview Developers. Since pressing share in magazine requests to open new window, it is required to allow webview to handle this case.
For webview we need to enable following settings
WebView webView = new WebView(getContext());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
The next step should be actual handling of opening of the request new window. It's handled via WebChromeClient.onCreateWindow. Here some code example
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView webView = new WebView(mContext);
//add webView to your view hierarchy
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(webView);
resultMsg.sendToTarget();
return true;
}
//Also don't forget to handle closing of the webview when page requested it in WebChromeClient.onCloseWindow.
//Handling of the URI related to specific app schema can be done in WebViewClient.shouldOverrideUrlLoading (looks like we have this as one of the share option).
//For example
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// if the url scheme is one of the following - do not override the url loading
if (URLUtil.isNetworkUrl(url.toLowerCase())) {
return false;
} else {
// try to launch app
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
}
}
Use video player in webview
Implementation needs WebChromeClientCustomPoster implementation and to attach it into your webview as shown in the example below.
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class PlayerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
WebView.setWebContentsDebuggingEnabled(true);
WebView v = this.findViewById(R.id.playerWebView);
v.setWebChromeClient(new WebChromeClientCustomPoster());
WebSettings webSettings = v.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
/**
* Make sure to apply this on the webview settings.
* Required for auto play without user gesture in some scenarios.
*/
webSettings.setMediaPlaybackRequiresUserGesture(false);
v.loadUrl("https://someurl.com");
}
/**
* This will hide the default webview video poster (gray play square)
* Make sure you are setting the web chrome client on the WebView instance
* With this one. e.g. webViewInstance.setWebChromeClient(newWebChromeClientCustomPoster());
*/
private class WebChromeClientCustomPoster extends WebChromeClient {
@Override
public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
}
}
}
Last updated