I want to open glauniversity.in in my webview. I have implemented following code and searched a lot, but everything seems futile. Any Help will be Appreciated. The code goes as follows:
public class WebViews extends Activity {
String link;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
link="glauniversity.in";
webview = findViewById(R.id.wv);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
webview.setWebChromeClient(new WebChromeClient() { #Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } });
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(link);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webview.canGoBack()) {
webview.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
glauniversity.in, This is not a valid url
replace this to
http://glauniversity.in/
Bonus::
Check valid url by
Patterns.WEB_URL.matcher(url).matches()
or
URLUtil.isValidUrl(url)
Related
I am doing webview in android in which I am calling a website link in that website I am calling web.whatsapp.com link. Now this link is working good in desktop as well as mobile view while the same link if I open in webview then showing error i.e. web page is not available for more clear let see the error image
Here is the code of webview android
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
bar =(ProgressBar) findViewById(R.id.progressBar2);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://.../");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
bar.setVisibility(View.GONE);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Try to override shouldOverrideUrlLoading in your web client and use code given below .
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
overrideUrlLoading = true;
} else {
view.loadUrl(url);
}
return overrideUrlLoading;
}
Hi I am developing android application in which I am using one web view. My web view has some data which redirect to some different link. So when I click it should come back to previous content. And finally it should close the web view when it comes to landing page. I tried it in following ways :
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
processBackPressed();
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (failingUrl.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
processBackPressed();
} else {
showError("Server unreachable");
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
#Override
public void onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack();
} else {
processBackPressed();
}
}
So problem is that whenever it is coming back to previous page it is fist showing current page and then previous page.So looks like first refresh the page and then load previous url. Am I doing anything wrong. Need Some Help. Thank you.
Try this -
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()){
myWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed (){
WebView webview = (WebView) findViewById(R.id.mainWebView);
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
super.onBackPressed();
finish();
}
}
private class MyCustomWebViewClient extends WebViewClient {
ProgressDialog progressDialog = new ProgressDialog(AndroidMobileAppSampleActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.setMessage("Processing");
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.setMessage("Processing");
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
This is my code, i am loading a url in webview and showing a processing dialog when user redirect to another page, Now i want to stop my page to redirect and also hide processing dialog when user press the back key. I tried but not successful, Please help. Thanks in advance
Clear history on finished page load.
No back history.
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
mWebView.clearHistory();
super.onPageFinished(view, url);
}
});
I'm developing an application in which I'm using webview to load certain websites. I also have customized actionbar on which I have website navigation button. Here is the view how my layout is looking like:
When I load website my all three buttons are visible, but I want them to be visible at that time only when user has clicked on some URL, so user easily understand the purpose of those buttons.
I have added all the functionalities to those button. Here is my code:
website_back = (ImageButton) cView.findViewById(R.id.website_back);
website_forward = (ImageButton) cView.findViewById(R.id.website_forward);
website_refresh = (ImageButton) cView.findViewById(R.id.website_refresh);
if (v.getId() == R.id.website_back) {
webview.goBack();
}
if (v.getId() == R.id.website_forward) {
webview.goForward();
}
if (v.getId() == R.id.website_refresh) {
webview.reload();
}
My WebViewClient:
private class HelloWebViewClient extends WebViewClient {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
website_back.setImageResource(R.drawable.webview_back);
view.loadUrl(url);
return true;
}
}
How can I only show those buttons when user has clicked on some URL, on the website launched in the webview?
Any kind of help will be appreciated.
You need to attach a WebViewClient to the WebView to listen in on the URLs that are being fetched.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals(mOriginalUrl)){
//initial url load, ignoring.
}
else {
//url was navigated to - do something
}
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
//page has finished loading
}
});
this worked for me. hope it would be useful for you. try this
ExternalSiteActivity.class
public class ExternalSiteActivity extends Activity{
WebView webView;
String productURL;
ProgressBar progressBar;
ImageButton webBackButton;
ImageButton webForwButton;
ImageButton webRefreshButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.external_site_activity);
productURL= (String) getIntent().getStringExtra("productUrl");
System.out.println(productURL+"**********");
progressBar=(ProgressBar)findViewById(R.id.progress);
progressBar.setVisibility(View.INVISIBLE);
webView = (WebView) findViewById(R.id.webview);
webBackButton=(ImageButton)findViewById(R.id.web_back_button);
webForwButton=(ImageButton)findViewById(R.id.web_forw_button);
webRefreshButton=(ImageButton)findViewById(R.id.web_refresh_button);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(productURL);
webBackButton.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick(View v) {
if(webView.canGoBack())
webView.goBack();
}
});
webForwButton.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick(View v) {
if(webView.canGoForward())
webView.goForward();
}
});
webRefreshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.reload();
}
});
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.v("shouldOverrideUrlLoading", "shouldOverrideUrlLoading");
return true;
}
#Override
public void onFormResubmission(WebView view, Message dontResend,
Message resend) {
Log.v("onFormResubmission", "onFormResubmission");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
if(webView.canGoBack())
{
webBackButton.setEnabled(true);
}
else
{
webBackButton.setEnabled(false);
}
if(webView.canGoForward())
{
webForwButton.setEnabled(true);
}
else
{
webForwButton.setEnabled(false);
}
progressBar.setVisibility(View.VISIBLE);
Log.v("onPageStarted", "onPageStarted");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onLoadResource(WebView view, String url) {
Log.v("on load ","load resources----");
}
// To handle "Back" key press event for WebView to go back to previous screen.
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I have a webview in my app. I can see the title of the website on the title bar (I have custom title bar). However, i dont see anything in the view - the website is not viewable :(.. any suggestions ? Here's the code:
public class WebViewer extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
webView = (WebView) findViewById(R.id.webview);
String url = "http://www.google.com";
final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
title.setText("Loading...");
WebViewer.this.setProgress(progress * 100);
if(progress == 100)
title.setText(webView.getTitle());
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
This isn't needed. shouldOverrideUrlLoading is called before the url is loaded to give you a chance to handle loading yourself. What you're doing is loading the url over and over.
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29