Mostar image if you are without internet connection

Show an image if the device is out of internet connection

Good afternoon gentlemen I'm new to Android programming, I'm using Android Studio

Next I am developing an app that simply calls a webVier, which directs to my website, and I would like to know how to make if the Mobile is without internet connection instead of calling the webvier it opens an image. my code on manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And I'm using here in main activy

import androidx.appcompat.app.AppCompatActivity;

Import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient;

Public class MainActivity extends AppCompatActivity {

private WebView delivery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    leve=findViewById(R.id.site);
    leve.getSettings().setJavaScriptEnabled(true);
    leve.setFocusable(true);
    leve.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    leve.getSettings().setAppCacheEnabled(true);
    leve.getSettings().setDomStorageEnabled(true);
    leve.setWebViewClient(new WebViewClient());
    leve.loadUrl("https://meusite.com.br");
}

}

I hope it was clear Thank you for now

Author: Rick Almeida, 2020-05-11

1 answers

An option, would be in your activity to check to see if you have internet( Google example) and if you do not have, instead of inserting an image in the webView, I would use an ImageView with your connection problem image(or with something else I want to show) and control the visibility of one and the other based on the connection. If I have Webview connection.visibility = true (I can leave the ImagemView by default in false) if I have no connection makes Webview.visibility = false and ImageView.visibility = true

 0
Author: Avallone, 2020-05-12 10:27:29