Inside a webpage I try to open the following html and it works. As you can see it contains a list of videos. All this videos work perfectly when I open them inside my browser, but I fail to open them from my device. Practically they don't play!!!!
This is my webpage....each item is a link to a video...
After I click one of this items I get here, this is the video...but it just doesn't play.
IMPORTANT The video doesn't play even if I click on it, whatever I do...it just won't play.
And here is my code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.second);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new HelloWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.loadUrl("Here is set the link");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("Here is set the link")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setDataAndType(Uri.parse(url), "video/*");
view.getContext().startActivity(intent);
return true;
}
else{
return super.shouldOverrideUrlLoading(view, url);
}
}
}
IMPORTANT The videos work inside of a browser. Anyone can give me a solution in order for my videos to play? Thank you
Consider using a WebChromeClient instead.
private class HelloWebViewClient extends WebChromeClient {
//Code here
}
Related
When external links are clicked on my website, I want them to open in the application it is connected to. But I am getting page not found error. For example, when instagram.com is clicked, it will open on Instagram.
You need to define a webView CLient like this:
private class MyWebViewClient extends WebViewClient {
//link opener
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("yourHost.com".equals(Uri.parse(url).getHost()))
{
// This is my website, so do not override; let my WebView load the page
//loading.show();
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
on set this webViewClient in your webView like: mywebView.setWebViewClient(new MyWebViewClient())
I am using the following code found on stackoverflow to make an app for my website.
It works, the website loads, but all links are loading inside the app.
I dont want links like facebook, youtube or any other external links to open inside my app.
How can I open external links outside my webview app ?
I am new to android coding, so if you can edit my code and post a full example answer, it will be great help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://www.sitename.com");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient{
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if((String.valueOf(request.getUrl())).contains("facebook")) {
view.loadUrl(String.valueOf(request.getUrl()));
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
}
return true;
}
}
So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.
I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});
I am doing one app. In this I am passing html file using WebView it is working good. But in html page I have some links. When I click that link means it is default going to website. But when I click that time no need to go to website. I need to go some activity like OnlineQuery.java. But I dnt knw how to move to that java file. any one knows please help me.
Java file:
public class DrugOffences extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setBackgroundColor(Color.BLACK);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/Drug.html");
}
}
Set a WebViewClient to your webview and override shouldOverrideUrlLoading. Check if the url to be loaded is the one you want to intercept. If it is do what you want and return true, if its not, return false.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("theURLYouDontWantToLoadInBrowser")) {
//Do your thing
startActivity(new Intent(this, OnlineQuery.class));
return true;
} else {
return false;
}
});
I don't understand at all your question, but if what you want is to handle the url navigation of the html load in the java code (webview), you could implement shouldOverrideUrlLoading (in this method you can intercept the url loadings) function.
Hope this helps
Try this..
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);