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);
Related
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 created an android app with webview.
Within the web page I have amazon banners.
If a user clicks on that banner I would like it to be open in the native android browser and not in webview.
I've tried several methods after searching and trying with no success.
When the banner is clicked - the app still shows the amazon site in webview mode.
Can anyone help me fix this issue ?
Your help is greatly appreciated...
Below is the code I use :
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
view.loadUrl(url);
loadAd();
return true;
}
});
webView.loadUrl("http://starter_url");
loadAd();
The function shouldOverrideUrlLoading() should:
Returns True if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
So you have to change your code to something like:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});
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;
}});
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
}
I have created a page which has link to a page of a website. So for showing that on I have used a WebView and it works fine.
My Problem is that when I click on any link given on that webpage, the link opens in phone's default browser view. But I want all the links to be opened in my created WebView.
Have I made any mistake or it is right..
Please Help Me
My code is as follows...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("------------", ".........................................................................................");
setContentView(R.layout.terms_of_use_web_view_page);
btn_back = (Button) findViewById(R.id.terms_of_use_button_back);
btn_back.setOnClickListener(this);
webview = (WebView)findViewById(R.id.terms_of_use_webview);
webview.getSettings().setJavaScriptEnabled(false);
webview.loadUrl("http://www.oomphlink.com/terms-of-use/");
}
Try specifying your own WebViewClient:
WebView webView = (WebView)findViewById( R.id.terms_of_use_webview );
webView.setWebViewClient( new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url )
{
view.loadUrl( url );
return true;
}
});
To further understand why this is necessary have a look at the documentation for the shouldOverrideUrlLoading method.
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient{
}
link for more info...