webview Hide or Replace "webpage not available" when offline - java

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl(" my site ");
webView.setWebViewClient(new WebViewClient(){});
}
}
How can I hide or replace webpage not available page?(please let me know where to put the code to hide or replace it also)
Thanks

You can check wether is there any connection(Wi-Fi or 3G) before loading to WebView.
Create a Utility class(Say AppUtility.java) write this method
/**
* Determine connectivity. a utility method to determine internet
* connectivity this is invoked before every web request
*
* #param ctx
* the ctx
* #return true, if successful
*/
public static boolean determineConnectivity(Context ctx) {
ConnectivityManager manager = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info != null && info.getState() == NetworkInfo.State.CONNECTED;
}
So now check the connectivity before loading page in WebView
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){});
if(AppUtility.determineConnectivity())
webView.loadUrl(" my site ");
else
Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
Hope this will help.

Related

How can I handle mailto and tel in android webview

I already make a java WebView for my website, this website in the footer the phone number and the email address when I click in it they told me (the webpage at tel could not be loaded because err_unknown_url_scheme), What should i do ?
MainActivity.Java
package xxx.xxxxxxx.xxxxxxxxx;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
Objects.requireNonNull(this.getSupportActionBar()).hide();
}
catch (NullPointerException ignored){}
//find the view
webView = findViewById(R.id.webview);
// to load all the web page in app itself use this
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.ezyadvs.com");
WebSettings webSettings =webView.getSettings();
//if your website is using any javascript that needs to load some script then you need to enable javascript in android application
webSettings.setJavaScriptEnabled(true);
}
#Override
public void onBackPressed() {
// if any previous webpage exist then onback pressed will take care of it
if(webView.canGoBack())
{
webView.goBack();
}else{
//else it will exit the application
super.onBackPressed();
}
}
}
Here is my solution in .kt (kotlin) :
this.webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
//add condition startsWith("tel:") or startsWith("mailto:")
if (url!!.startsWith("whatsapp:")) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url)
startActivity(intent)
return true
}
return false
}
}

Whatapp share from Android webview is not working

I'm new to Android & I know this is repeatedly answered question & I found lot of conversations on this question but finally same answer. When I tried that answer, it is not working.
I converted my website into Android app, it is working fine !! when I tried to share the link from my webview to whatsapp, it is giving below error
net::ERR_UNKNOWN_URL_SCHEME
To fix this, I have seen many suggestions with shouldOverrideUrlLoading. I tried but no luck
My Code:
package com.example.abcdefgh;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
public
class MainActivity extends AppCompatActivity {
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("http://www.abcdefgh.com");
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient {
#Override
public
boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} else {
return false;
}
}
}
#Override
public void onBackPressed() {
if (mywebView.canGoBack()) {
mywebView.goBack();
} else {
super.onBackPressed();
}
}
}
I don't know what is the issue, please help me

open native maps from webview

I have an app with a webview. The webview links to a mobile webpage where all links work correctly. When I click on a link in this webview, the link opens up in google maps inside the webview. Instead, I want the link to open up the native google maps app. Can anyone tell me how to do this?
I have this code that I need to insert it to my code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("geo:")) {
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}
view.loadUrl(url);
return true;
}
This is my code:
package com.company.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("www.google.com");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
}
}
To call android method from javascript you would need to make Javascriptinterface class and annotating method like #JavascriptInterface in documentation
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
and bind it to webview like
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
and from javascript you can call it like
function showtoastfromsite(){
Android.showToast("hello from website");
}

Add button to actionbar in android activity

I would like to add a share button in an action bar of my current activity.
I don't know how to do it.
Is it possible to do this in activity ?
Here is the code of my activity :
package com.rss.utils;
import com.rss.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebBrowserViewActivity extends Activity {
WebView webview;
ProgressBar progressB = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_browser_view);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
Log.d("WebBrowserViewActivity", "URL to load : " +url);
progressB = (ProgressBar) findViewById(R.id.progressBar1);
webview = (WebView) findViewById(R.id.webViewArticle);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
// webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress < 100 && progressB.getVisibility() == ProgressBar.GONE){
progressB.setVisibility(ProgressBar.VISIBLE);
}
progressB.setProgress(progress);
if(progress == 100) {
progressB.setVisibility(ProgressBar.GONE);
}
}
});
webview.loadUrl(url);
}
}
Thanks a lot for your help.
++
I've never done that before but I did some research for you and I'm pretty sure this is what you're looking for.
This article will tell you what to do :)
In addition to what #Matthew said, this answer on question How to track the Share button clicked on the ActionBar? provides step wise details to do the same. Some of other links that may help are: How to activate "Share" button in android app? and Adding share action in ActionBar. Hope this helps.

onKeyDown method crashing app

I have the following code that is working but when I hit the hardware back button the app crashes,
package com.fnesse.tmpmobile;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressWarnings("unused")
public class TmpmobileActivity extends Activity
{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(0x00000000);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
//Error Code Here
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.themorningtonpeninsula.com");
}
#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 found somewhere that I needed to add,
WebView webView;
Above the onCeate method, which allowed webView to resolve. So now I get no errors but it crashes and the app runs on my phone but crashes when I hit the hardware back button.
Does anyone know why that would be happening?
Cheers,
Mike.
I'll try to do my magic guest on the error message. I think, this error is caused by the way to initialize webView variable. You see, on the class level, you declare:
WebView webView;
And then, on onCreate() you initialize webView and also redeclare it again:
WebView webView = (WebView) findViewById(R.id.webview);
But, this redeclare step resulted in class level webView to be uninitialized.
To fix it, try to change this line:
WebView webView = (WebView) findViewById(R.id.webview);
Into:
webView = (WebView) findViewById(R.id.webview);
And you should now have class level webView initialized and ready for use in whatever method you have.
Here's few resources explaining Java variable scope:
http://www.java-made-easy.com/variable-scope.html
http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm
You are creating a new Webview in your onCreate() method instead of the Class field Webview. Change
WebView webView = (WebView) findViewById(R.id.webview);
into:
webView = (WebView) findViewById(R.id.webview);

Categories