Home > front end >  Reload WebView on Button Press Android Studio
Reload WebView on Button Press Android Studio

Time:05-15

I have a WebView inside my Android Studio project. It fills the whole screen, and when they get into the app without an internet connection I hide the WebView and show text and a button telling them they don't have internet, and allowing them to reload the page. How can I have the button reload the WebView when it is pressed? Reloading the whole app would work as well but as a last resort.

This is my activity-main.xml file:

<?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"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

    </WebView>

    <LinearLayout
        android:id="@ id/no_internet_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <TextView
                android:id="@ id/alertTitle"
                android:layout_width="match_parent"
                android:layout_height="114dp"
                android:fontFamily="sans-serif-medium"
                android:text="You don't have an internet connection."
                android:textAlignment="center"
                android:textSize="34sp"
                tools:layout_editor_absoluteX="3dp"
                tools:layout_editor_absoluteY="0dp" />

            <TextView
                android:id="@ id/alert"
                android:layout_width="match_parent"
                android:layout_height="159dp"
                android:text="[This site] requires you  to have an internet connection."
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@ id/reloadButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Reload" />
        </LinearLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

I am assuming that you are using Java. I have made an example for you:

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

        webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("url");

        Button button = (Button) findViewById(R.id.reloadButton);
        
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                
                webview.reload();
                
            }
        });
    }

When the button is pressed, the webview reloads.

CodePudding user response:

 button.setOnClickListener(new OnClickListener() {
        
        public void onClick(View v) {
            
           webview.loadUrl("url");
            
        }
    });
  • Related