SwipeRefreshLayout to refresh WebView

Suppose you have an app which contains a WebView webview1 inside a LinearV linear1. In onCreate it loads a website and onBackPressed it goes to previous page.

To implement SwipeRefreshLayout in this app to reload the WebView, follow the steps given below.

1. Swith on AppCompat and design.

2. Create a more block extra and put codes to declare a SwipeRefreshLayout srl.
Copy code
}
androidx.swiperefreshlayout.widget.SwipeRefreshLayout srl;
{


3. In onCreate event, before loading url in WebView;
* define a new SwipeRefreshLayout srl,
* remove all views from linear1,
* add SwipeRefreshLayout to linear1,
* add WebView to SwipeRefreshLayout,
setOnRefreshListener for the SwipeRefreshLayout,
* In onRefresh event of OnRefreshListener, load WebView getUrl in WebView using blocks.

Copy code
srl = new androidx.swiperefreshlayout.widget.SwipeRefreshLayout(this);

linear1.removeAllViews();
linear1.addView(srl);
srl.addView(webview1);

srl.setOnRefreshListener( new androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener() {
@Override public void onRefresh() {

// PUT BLOCK TO REFRESH WEBVIEW HERE

}});

4. Add WebView onPageFinished event. Here, to stop the SwipeRefreshLayout, use setRefreshing(false).


Copy code
srl.setRefreshing(false);

5. Save and run the project.

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

TextInputLayout in Sketchware

How to find and​ highlight a word in a text field in Sketchware?

A Flash Light App in Sketchware

How to enable download in webview in Sketchware apps?

Intent - Open File By Type

Code for implementing Notifications in Sketchware

How to share an image from Drawable folder?

ActionBar back button

Animation Transtition Animation