Custom NoInternet In Andriod Studio But image and layout not scale to fit

Viewed 38

I used this code to make custom No internet dialog but when i successfully build then the problem is half screen show my custom image but half show the no connection errorenter image description here

In Activity_main.xml enter image description here

 <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
    android:id="@+id/swipeRefreshLayout"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ProgressBar
            android:id="@+id/ProgressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="9dp"
            android:layout_marginTop="-2dp"
            android:progress="20"
            android:visibility="gone" />
        <ImageView
            android:id="@+id/splashloading01"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/todo"
            android:src="@mipmap/splash_loading"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone">

        </ImageView>



        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" >

        </WebView>


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayout">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/internet"
                android:layout_centerHorizontal="true"
                android:id="@+id/noConnectionLogo"
                android:contentDescription="@string/todo" />
              <Button
                android:layout_width="140dp"
                android:layout_height="55dp"
                android:text="Join"
                android:id="@+id/btnNoConnection">

              </Button>



        </RelativeLayout>

</LinearLayout>

In MainActivity.xml my code is

package www.foodmartshop.com;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    RelativeLayout relativeLayout;
    Button btnNoInternetConnection;
    ProgressBar progressBarWeb;
    SwipeRefreshLayout swipeRefreshLayout;



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

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        //Button No Internet//
        btnNoInternetConnection = (Button) findViewById(R.id.btnNoConnection);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //No Internet//
        webSettings.setJavaScriptEnabled(true);
        progressBarWeb = (ProgressBar) findViewById(R.id.ProgressBar);

        //Swipe//
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mWebView.reload();
            }
        }); //Swipe//
        mWebView.loadUrl("https://www.foodmartshop.com");
        mWebView.setWebViewClient(new www.foodmartshop.MyAppWebViewClient() {


            //Hide The Loading Page
            @Override
            public void onPageFinished(WebView view, String url) {
                findViewById(R.id.splashloading01).setVisibility(View.GONE);
                findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
                swipeRefreshLayout.setRefreshing(false);

            }


        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                progressBarWeb.setVisibility(View.VISIBLE);
                progressBarWeb.setProgress(newProgress);

                if (newProgress == 100) {

                    progressBarWeb.setVisibility(View.GONE);

                }

                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()){
            mWebView.goBack();
        }
        else{
            super.onBackPressed();
        }
    }

    private class MyAppWebViewClient extends WebViewClient {

    }

    public void checkConnection() {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isConnected()) {
            mWebView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.GONE);
        }


        else if (mobileNetwork.isConnected()){
            mWebView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.GONE);
    }

        else{
            mWebView.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.VISIBLE);
        }
    }


}

In Mamifest my code is :

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="www.foodmartshop.com">


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:usesCleartextTraffic="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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Plz help me i didnt understand the problem

1 Answers

try this---->

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    //Button No Internet//
    btnNoInternetConnection = (Button) findViewById(R.id.btnNoConnection);
    relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //No Internet//
    webSettings.setJavaScriptEnabled(true);
    progressBarWeb = (ProgressBar) findViewById(R.id.ProgressBar);
    checkConnection();
  //  mWebView.loadUrl("https://www.foodmartshop.com");

    //Swipe//
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            checkConnection();
            mWebView.reload();

        }
    }); //Swipe//
  //  mWebView.loadUrl("https://www.foodmartshop.com");
    mWebView.setWebViewClient(new MyAppWebViewClient() {


        //Hide The Loading Page
        @Override
        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.splashloading01).setVisibility(View.GONE);
            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
            swipeRefreshLayout.setRefreshing(false);

        }


    });

    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
     checkConnection();
            progressBarWeb.setVisibility(View.VISIBLE);
            progressBarWeb.setProgress(newProgress);

            if (newProgress == 100) {

                progressBarWeb.setVisibility(View.GONE);

            }
            else
            {
                checkConnection();
            }

            super.onProgressChanged(view, newProgress);
        }
    });
}

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

private class MyAppWebViewClient extends WebViewClient {

}

public void checkConnection() {
    ConnectivityManager connectivityManager = (ConnectivityManager)
            this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (!wifi.isConnected()) {
    progressBarWeb.setVisibility(View.GONE);
        mWebView.setVisibility(View.GONE);
        relativeLayout.setVisibility(View.VISIBLE);
    } else if (!mobileNetwork.isConnected()) {
        progressBarWeb.setVisibility(View.GONE);

        mWebView.setVisibility(View.VISIBLE);
        relativeLayout.setVisibility(View.GONE);
    } else {
        progressBarWeb.setVisibility(View.GONE);

        mWebView.loadUrl("https://www.foodmartshop.com");

        mWebView.setVisibility(View.VISIBLE);
        relativeLayout.setVisibility(View.GONE);
    }
}
}

later you must be noticing progressbar loads continuously even if internet is not there then you must use this-->

just an example -->

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);


    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        progressDialog.hide();
        progressBar.setVisibility(View.GONE);
    }
});
Related