How to open external player when using WebView? - android

How to open external video player when using WebView on JS site? I try to open an external video player through the android application on the webview, but nothing works.
file MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.loadUrl("https://google.com/");
WebViewClient webViewClient = new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
};
webView.setWebViewClient(webViewClient);
}
};
I tried using this code but nothing comes out
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(".avi") || url.contains(".ts") || url.contains(".mkv") || url.contains(".mp4")){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
startActivity( intent );
return true;
}else {
return false;
}
}

Related

WhatsApp custom url scheme not working in a Webview

I am doing webview in android in which I am calling a website link in that website I am calling web.whatsapp.com link. Now this link is working good in desktop as well as mobile view while the same link if I open in webview then showing error i.e. web page is not available for more clear let see the error image
Here is the code of webview android
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
bar =(ProgressBar) findViewById(R.id.progressBar2);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://.../");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
bar.setVisibility(View.GONE);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Try to override shouldOverrideUrlLoading in your web client and use code given below .
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
overrideUrlLoading = true;
} else {
view.loadUrl(url);
}
return overrideUrlLoading;
}

How to open .ppt, .pptx, .doc, .xlsx files in a webview from the SD card?

"How to open .ppt, .pptx,.doc, .xlsx files in android webview within app?"
"This is requirement to open any documents which i have download in sd card and open witin android webview. In the past I have tried lot of google suggested answer which they are open file outside the app using intent and MIME Type or open online file like webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/mySlide.ppt");"
public class WebContentViewActivity extends BaseActivity {
#BindView(R.id.webView)
WebView webView;
private String docUrl="http://docs.google.com/gview?
embedded=true&url=";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder builder = new
StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
setContentView(R.layout.activity_video_view);
ButterKnife.bind(this);
WebSettings settings = webView.getSettings();
webView.clearCache(true);
webView.clearHistory();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebViewClient(new WebViewController(this));
filePath =
"/storage/emulated/0/New_Files/1550130536314somefile.pptx";
fileName = "1550130536314somefile.pptx";
if (!TextUtils.isEmpty(fileName )){
Uri fileUri = Uri.fromFile(fileName );
webView.loadUrl(fileUri.toString());
// webView.loadUrl(docUrl+fileUri);//getting 400.That's an error
on webview
}
/**
* Custom WebView controller class
*/
private class WebViewController extends WebViewClient {
private Context mContext;
//TODO : use this flag while open clicked url in the same WebView
public WebViewController(Context ctx) {
this.mContext = ctx;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
System.out.println("onPageStarted: " + url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.v("Webview Error", description);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
{
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I want to show any document file witin webview as above in the code.

i have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

Telephone link in webview is opening in browser

I have a telephone link on my webview app: tel:062123658 but when I click on it I get webpage not found.
This is my code:
public class FullscreenActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://www.mywebsite.nl");
webView.setVerticalScrollBarEnabled(false);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
How can I fix this?
You should override a function in WebViewClient:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadData("Hello World, 317.456.2564 ", "text/html","utf-8");
//webView.loadUrl("http://www.techjini.com/contactus.html");
webView.setVerticalScrollBarEnabled(false);
}
class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
}
Try this way:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
});

WebView shouldOverrideUrlLoading Wont Play Video

My app displays a webpage using a WebView. I want the user to be able to click on a link to a video, and the video be played in landscape mode. Where I am now, the video doesn't even play??
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mnwv_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
Change this:
...
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
with this:
...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
});
myWebView.loadUrl("http://www.meanwhileinwv.com");
}

Categories

Resources