Mostar image if not connected to the internet

Asked

Viewed 50 times

1

Show a picture if the device is not connected to the internet

Good afternoon gentlemen I’m new in Android programming, I’m using Android Studio

Next I am developing an app that simply calls a webVier, which directs to my site, and I would like to know how to make if the mobile phone is not connected to the internet instead of calling the webvier it opens an image. my code on the 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 it here on 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 I’ve been clear Thank you for now

1 answer

0

One option, would be in your Activity do un check para ver se tem internet(google example) and if not, instead of inserting an image into webView, I would use an Imageview with its connection problem image (or something else you want to show) and control the visibility of both based on the connection. If I have Webview.visibility = true connection (I can leave Imagemview by default in false) if I have no connection makes Webview.visibility = false and Imageview.visibility = true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.