Home > Software design >  How can I make my button in disappear when there's no internet?
How can I make my button in disappear when there's no internet?

Time:01-20

Hello I'm trying to get my button that I have in my main XML file to only appear when there's no internet So that's why I can get the button to only appear when there's no internet and so it's not shown anywhere else unless there's internet can someone please help me?

Blow i have listed the code for both the main XML and the main Java file

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">


    <WebView
        android:layout_width="10dp"
        android:layout_height="10dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="200dp"
        tools:layout_editor_absoluteY="361dp"/>

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="202dp"
        tools:layout_editor_absoluteY="399dp" />


</androidx.constraintlayout.widget.ConstraintLayout>\

java

public class MainActivity extends AppCompatActivity {

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


        WebView webView = new WebView(this);
        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
        setContentView(webView);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);


        if(isNetworkAvailable(this)){

            webView.loadUrl("https://racks.tk");

        } else {


            Toast.makeText(this,"No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();

        }
    }

    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
                return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            } else {
                NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
                return activeNetwork != null && activeNetwork.isConnected();

            }
        }

        return false;
    }

CodePudding user response:

You are creating a web view programmatically and then trying to access a button from an XML file and you have not linked the XML file in the Java file, so it crashes. Update your code with this code completely

XML file - activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">


    <ProgressBar
        android:id="@ id/my_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true"/>

    <WebView
        android:id="@ id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:id="@ id/reload_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Retry" />


</RelativeLayout>

java file - MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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


        WebView webView = findViewById(R.id.my_webview);
        Button reloadBtn = findViewById(R.id.reload_btn);
        ProgressBar progress = findViewById(R.id.my_progress);

        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);


        webView.setWebViewClient(new WebViewClient(){

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

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progress.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });


        if (isNetworkAvailable(this)) {

            webView.loadUrl("https://racks.tk");

            reloadBtn.setVisibility(View.GONE);

        } else {

            Toast.makeText(this, "No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();

            reloadBtn.setVisibility(View.VISIBLE);

        }


        reloadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isNetworkAvailable(MainActivity.this)){
                    reloadBtn.setVisibility(View.GONE);
                    webView.loadUrl("https://racks.tk");
                }else {

                    reloadBtn.setVisibility(View.VISIBLE);
                    Toast.makeText(MainActivity.this, "No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
                return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            } else {
                NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
                return activeNetwork != null && activeNetwork.isConnected();

            }
        }

        return false;
    }
}

CodePudding user response:

You are missing the @Override :

Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override //Right here
        public void onClick(View v) {
            webView.loadUrl( "javascript:window.location.reload( true )" );
        }
    });

CodePudding user response:

Button button= findViewById(R.id.button);

if(isNetworkAvailable(this)){

        webView.loadUrl("myurl.com");

        button.setVisibility(View.VISIBLE)

    } else {

        button.setVisibility(View.GONE)
        Toast.makeText(this,"check internet connection and try again", Toast.LENGTH_LONG).show();

    }
  •  Tags:  
  • Related