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.
Related
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;
}
}
Im Creating a Simple webapp that Displays a Login Page of my Institution. The Page is Written in .aspx When i Click Login After Entering my Login Details , It Shows Me to use any Browser App to Open that Page, is There any Way to open the page in same app ??
public class portalview extends Activity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.portal);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://portal.saveetha.com/deptweb/home/Login.aspx");
}
}
Try this to implement WebViewClient() and set like:
WebView webView = (WebView)this.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://portal.saveetha.com/deptweb/home/Login.aspx");
I just start android developing and I want to embed my website in the app.
When I want to open "http://www.google.com" , webpage opens in My app, but when I change address to my blog it wants to open it on external browser.
This is My activity code that I used to embed my site!
public class WebPage extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
//web view start
WebView med = (WebView) findViewById(R.id.webView1);
med.getSettings().setJavaScriptEnabled(true);
med.getSettings().
med.loadUrl("http://www.mediratour.com");
}
}
My webpage based on wordpress, I don't know if I have to change settings to prevent using external browser and opens it in My app.
Thanks
// Set this on your web view.
webView.setWebViewClient(new WebClient());
// Create this class.
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
webView.loadUrl(url);
return true;
}
}
I think it helps.
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);
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...