in some activities it shows a website contained in a WebView, in cases where the device has no data or is not connected to a Wi-Fi connection, I would like to show a nice message, possibly with an image where the user is told what to see content needs internet connection.
For example in the following view:
public class Fragment_Home extends Fragment {
public Fragment_Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
String url="http://www.uniagustiniana.edu.co/";
WebView view=(WebView) v.findViewById(R.id.wv_home);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient());// Agregamos un WebViewCliente, esto permite que se sigan ejecutando los links dentro de este WebView
view.loadUrl(url);
return v;
}
}
xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tecnologias.uniagustapp.fragmentos.Fragment_Home">
<!-- TODO: Update blank fragment layout -->
<WebView
android:id="@+id/wv_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Currently when the computer does not have internet access it shows me this screen:
Thank you very much for the help.
You can add an html you want to display inside
/assets
and load it when there is no connectivity.The file in this case is added inside :
If you need to know if there is internet available, you should check if there is connectivity with isAvailable() and if there is connectivity and it is possible to establish a connection with isConnected()
Using the above method we can determine if there is connectivity and if it is possible to establish a connection.
Remember to use
getActivity()
as context if you are inside aFragment
,if you are in one
Activity
you can use the application context bygetApplicationContext()
Very important to add the permissions, to determine the network status and have internet permissions:
To know if there is an Internet connection, use the
ConnectivityManager
and classesNetworkInfo
, with these classes you will obtain the information you need to know if there is an Internet connection or not. Then, you just have to use a conditional , which if true loads the web page and if false loads an error message. You can load the error message from a url, an html file or a String.To use the classes
ConnectivityManager
andNetworkInfo
you have to add the following permissions to the AndroidManifest.xml :Fragment_Home
I want to contribute the method I use, seeing that the Android version has not been taken into account in the answers given.
Usage example: