Open new tabs on default browser when using webview - android

I want to tell you I never try android development before, and this is the first time I'm doing an android app I build a website using ASP.NET and I want to run that site within the android app web view.
So followed a few tutorials and I did so far.
Here when I redirect to some external sites In my site, I open them on another new tab on the browser, here I want to open it on the mobile phone's default browser instead of the opening inside the web view.
need a help to doing that.
This is my app MainActivity code.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.mycustom:5500/test/");
WebSettings websettings = webView.getSettings();
websettings.setJavaScriptEnabled(true);
}
private class MyWebView extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
}
#Override
public void onBackPressed() {
if (webView.isFocused() && webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
}

Related

App stops when calling Webview back to Activity Intent

I developed a simple app that has two activities. When I call Menu activity to Webview using intent, it opens very well in web view, but when I click the button to return back to Activity, it crashes and asks to "close the app." Below is my java and it looks fine without errors. The log cat as well doesn't display any errors.
Who has the solution to these?
public class WebView extends AppCompatActivity {
WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String url = getIntent().getStringExtra("site_url");
android.webkit.WebView webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("" + url);
}
private class MywebClient extends WebViewClient {
#Override
public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
}
#Override
public void onBackPressed() {
android.webkit.WebView webView = null;
if(webView.isFocused() && webView.canGoBack())
{
webView.goBack();
}else{
super.onBackPressed();
}
}
}
Your problem is right here, in onBackPressed():
android.webkit.WebView webView = null;
if(webView.isFocused() && webView.canGoBack())
You set the variable webView to null and then you call a method isFocused() on it. That will cause a NullPointerException.
Change this to:
android.webkit.WebView webView = findViewById(R.id.webView);
if(webView.isFocused() && webView.canGoBack())

How to get Webview hyperlink to open in webview app instead of browser

I am trying to build a Webview app using android studio and I am currently having a problem. The index page shows perfectly on the app but whenever i click a hyperlink it redirects me to the browser and then to the website how can I get it to redirect me to another page whilst in the app built from webview. Here is a snippet of the main activity java
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);
WebSettings websettings=mywebView.getSettings();
mywebView.loadUrl("https://www.cavaapperal.co.za/");
websettings.setJavaScriptEnabled(true);
}
public class myWebClient extends WebViewClient{
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed (){
if (mywebView.canGoBack()) {
mywebView.goBack();
} else{
super.onBackPressed();
}
}
}
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
WebView link click open default browser
Kindly see the following link

Access remote pdf file with login (authentication)

I am facing problem with loading URL in web-view. Web-view shows blank because it is not logged in. How to access course file without login into moodle2.6 by using url.
Using this url format
http://example.com/pluginfile.php/4418/mod_resource/content/10/xx-t2.pdf
Here is my web-view code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//progDailog.dismiss();
}
});
webView.loadUrl("http://example.com/pluginfile.php/4418/mod_resource/content/10/xx-t2.pdf");
You need to append the pdf URL to Google Doc Viewer to open pdf in web view, Also you will need to enable JavaScript for your webview
public class MainActivity extends AppCompatActivity {
WebView webview;
String pageURL = "https://developer.android.com/guide/webapps/webview.html";
String pdf = "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf"; //your pdf address
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptEnabled(true); // enable javascript
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//progDailog.dismiss();
}
});
//webview.loadUrl(pageURL);
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); //simply append pdf address to doc viewer
}
}
Sample PDF opened in webview
It seems that you're webclient is not calling their super methods, which could be one reason for the page to not load, use the following code to load the login page, which once signed in the user could view the intended pdf file.
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);//<-- this is important when overriding
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);//<-- this is important when overriding
}
});
webView.loadUrl("https://lms.jsbl.com//pluginfile.php//4925//mod_resource//content//0//calendar_cal_605.pdf");
And also If you want you view a pdf using the webview you need append the following before your url so that the google plugin could be used to load the pdf
https://docs.google.com/gview?embedded=true&url=
(for this to work make sure you have added the following to your webview
webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setPluginState(PluginState.ON);)
According to this question some login form submit might not work on android webview so set all/some(check if your login works without it) on your webview to make the login work
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDatabasePath(dbpath); //check the documentation for info about dbpath
mWebView.getSettings().setMinimumFontSize(1);
mWebView.getSettings().setMinimumLogicalFontSize(1);
But if you know the credentials for the login You could follow the instruction on this site to inject a javascript to the webview which would fire the Login form submit on the webview and redirect to the pdf file directly
UPDATE 16/3/2018
Use the following code which will append the google pdf widget during a redirect
and has a pdf extension
boolean isInititialRedirectComplete = false;
boolean isPdfRedirectComplete = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f-inal WebView webView = (WebView) findViewById(R.id.testWebView);
webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
super.onPageFinished(view, url);
if (url.substring(url.lastIndexOf(".")).equalsIgnoreCase(".pdf") && !isPdfRedirectComplete && isInititialRedirectComplete ){
isPdfRedirectComplete = true;
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + url);
}
isInititialRedirectComplete = true;
}
});
webView.loadUrl(
"https://lms.jsbl.com//pluginfile.php//4925//mod_resource//content//0//calendar_cal_605.pdf");
}

Android Webview doen't work correctly

This site works well in android browsers but it doesn't work in android webview.
I set internet permission and did all things.
Who can teach me?
public class MainActivity extends AppCompatActivity {
WebView web_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.webView);
getSupportActionBar().hide();
web_view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
web_view.getSettings().setDomStorageEnabled(true);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebViewClient(mWebViewClient);
web_view.addJavascriptInterface(new MyJavaScriptInterface(), "android");
web_view.loadUrl("http://182.72.55.182/pms/employee");
}
WebViewClient mWebViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
#JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
}
make sure that your regarding website becoming live in everywhere!
webview doesn't display any local host website
Try to put WebViewClient part inside the onCreate.

Android Webview not loading linkedin page

I am trying to load linkedin page in webview but i can only see linkedin logo but i tried loading facebook and it facebook successfully. Here is my code:
public class WebViewActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.loadUrl("https://www.linkedin.com");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Can anybody help me out. Thanks in advance.
You need to enable javascript in your WebView like this:-
web.getSettings().setJavaScriptEnabled(true);

Categories

Resources