uses permissions required
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
use xml file for webview
for show any view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linNtWkOff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:visibility="visible"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/img_ntwk_conn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="center_horizontal"
android:textColor="@color/black"
android:text="Internet Connection Unavailable"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
i have used webView using network check in baseActivity
//There are following parameters:
public class MainActivity extends BaseActivity {
private WebView mWebView;
LinearLayout linNtWkOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linNtWkOff = findViewById(R.id.linNtWkOff);
mWebView = (WebView) findViewById(R.id.webView);
}
@Override
protected void onResume() {
super.onResume();
if (checkInternetConnection()){
setNetWorkON();
}else {
setNetWorkOFF();
}
mWebView.setWebViewClient(new MyBrowser());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
mWebView.loadUrl("-----write your url------");
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void setNetWorkON(){
linNtWkOff.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
}
public void setNetWorkOFF(){
linNtWkOff.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.GONE);
}
private boolean checkInternetConnection() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
boolean isConnected;
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
isConnected =true;
} else {
isConnected= false;
}
return isConnected;
}
}
BaseActivity use for newwork connection check
public class BaseActivity extends AppCompatActivity {
private static final int WIFI_ENABLE_REQUEST = 0x1006;
private BroadcastReceiver mNetworkDetectReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
checkInternetConnection();
}
};
private AlertDialog mInternetDialog;
private boolean isConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isConnected = false;
registerReceiver(mNetworkDetectReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
protected void onDestroy() {
unregisterReceiver(mNetworkDetectReceiver);
super.onDestroy();
}
private void checkInternetConnection() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
isConnected =true;
//showNoConnectionSnackBar("Connected", isConnected, 1500);
onResume();
} else {
isConnected= false;
//showNoConnectionSnackBar("No active Internet connection found.", isConnected,10000);
onResume();
}
}
private void showNoConnectionSnackBar(String message, boolean isConnected, int duration) {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
message, duration);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView
.findViewById(R.id.snackbar_text);
textView.setTextColor(ContextCompat.getColor(this, android.R.color.white));
if (isConnected){
sbView.setBackgroundColor(getResources().getColor(R.color.purple_500));
}else{
sbView.setBackgroundColor(getResources().getColor(R.color.purple_500));
snackbar.setAction("Turn On", new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent internetOptionsIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(internetOptionsIntent, WIFI_ENABLE_REQUEST);
}
});
snackbar.setActionTextColor(getResources().getColor(R.color.purple_500));
}
snackbar.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == WIFI_ENABLE_REQUEST) {
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}