'Reload WebView on Button Press? (Android Studio)
I have a WebView inside my Android Studio project that fills the whole screen, and when they get into the app without an internet connection I show an activity that I have included, I hide the WebView, and I show text and a button telling them they don't have internet. 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"
android:background="@color/white"
android:backgroundTint="@color/white"
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">
<include
layout="@layout/no_internet"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is my no-internet.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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="54dp"
android:fontFamily="sans-serif-medium"
android:text="Sorry (user)!"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063"
tools:layout_editor_absoluteX="0dp" />
<TextView
android:id="@+id/errorMessage"
android:layout_width="match_parent"
android:layout_height="34dp"
android:text="Something went wrong with this app."
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@+id/space"
tools:layout_editor_absoluteX="0dp" />
<TextView
android:id="@+id/errorCode"
android:layout_width="match_parent"
android:layout_height="22dp"
android:text="ERROR_INTERNET_DISCONECTED"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/errorMessage"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/refreshButton"
android:layout_width="127dp"
android:layout_height="59dp"
android:text="Retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.854"
app:strokeColor="@color/blue_primary" />
<Space
android:id="@+id/space"
android:layout_width="237dp"
android:layout_height="37dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
And this is my MainActivity.java file:
package com.example.projectname;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
LinearLayout no_internet_view = (LinearLayout) findViewById(R.id.no_internet_view);//////this line for define no_internet_view
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webView.loadUrl("https://websitename.com");
webView.setWebViewClient(new WebViewClient());
if (isInternetOn()) {
webView.setVisibility(View.VISIBLE);
no_internet_view.setVisibility(View.GONE);
} else {
webView.setVisibility(View.GONE);
no_internet_view.setVisibility(View.VISIBLE);
}
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed(); //maybe shove option first later
}
}
public boolean isInternetOn() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
}
My reloadButton will be what should reload the page.
Solution 1:[1]
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.
Solution 2:[2]
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.loadUrl("url");
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Simon |
Solution 2 | Hassan holi |