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...
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 have a web page with a basic form (like a newsletter subscription).
The user sends its information and gets a success page with a link to the form submission page. If he doesn't click on the link the page will be automatically redirected after 5 seconds.
<meta http-equiv="refresh" content="5;url=www.site.com" />
So far so good! It works as expected in both desktop and mobile browsers.
Then I created an android app (min API SDK 21) with a web view to load the site.
The problem is that the auto refresh isn't working inside the web view...
Am I missing something? I'm trying to avoid the javascript hack...
Thanks for your time!
EDIT
I notice that when I click on the new form submission page link it opens the default browser instead of rendering it on the web view!
After searching for the problem, I came to a solution by resetting the URL on URL loading.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
}
It's working, but I think it can be improved.
My initial problem was that I wasn't setting a WebViewClient.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.loadUrl(url);
}
Thus, I think I just need to add a WebViewClient and it'll work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
Note, and if we want javascript to work we need to enable it
mWebView.getSettings().setJavaScriptEnabled(true);
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.
In my android app the user can like my facebook page. Therefore I use a webview that loads a webpage that contains a facebook like box. The page is loaded well into the webview and then the following happens:
WebView loads my webpage with a facebook like box
By clicking on the Like button the user is redirected to the facebook login page
After login the user is redirect again back to my custom like page
But when clicking the like button the user is again redirected to the facebook login page
So I would expect, that the like is possible after logging in. It seems somehow as if the webview does not remember the login. Therefore, what do I have to do to repair this.
The following screenshot sequence shows what's happening:
I want to avoid using the facebook sdk for android! Especially this procedure works very good in my iphone version of the app.
Here is some code that is used to implement my desired functionality:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView fbWebView = (WebView) findViewById( R.id.facebookWebView );
fbWebView.getSettings().setJavaScriptEnabled(true);
fbWebView.setWebViewClient( new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
Log.d("call url", url );
if ( url.contains("login.php?skip_api_login") ) {
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
return null;
}
if( url.contains("user_successfully_liked") )
fbWebView.loadUrl("http://www.example-success.de");
return null;
}
});
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
}
EDIT: I also tried the following to accept cookies but none of this worked
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.setAcceptFileSchemeCookies(true);
CookieManager.getInstance().setAcceptCookie(true);
//rest is the same...
Overrding didn't work either
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
You have that error, because WebView isn't remembering cookies. Read that question on SO : WebView and Cookies on Android
Not sure if you've gotten an answer to your problem, but I tried a combination of these instructions and the code below, and that worked for me:
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
...
WebView webView = new WebView();
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setGeolocationEnabled(true); // this last one is probably optional
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(<your url here>);
...
}
You will then need to call the corresponding methods for the CookieSyncManager singleton class as outlined in the link above. Note that the sync class is deprecated in API Level 21: see details on CookieSyncManager class
I've created a WebView app and everything works fine.
I'm new to Android.The only remaining feature I need for the app is to hide the address bar. Because I want the app to look more like a regular app and not a webpage within a web browser window.
My code is like this,
package com.Mobi.ebookread;
public class Mobile extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webview = (WebView) findViewById(R.id.helloWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
}
How do I do it?
i think there is a problem. webview does not have an address bar, seems that the browser app is getting opened. It could be possible that there is a redirection happening and that causes the browser app to open up, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading()
Below is how the webview looks like.
Finally I Try with this. Its worked for me..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient()); //use to hide the address bar
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true); //to store history
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should like this:
`<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>`
don't go to add layouts.
try the following code:
webView=(WebView) findViewById(R.id.helloWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.google.com");