2

Probably this is only a simple matter but please bear with me. I just started to learn about coding and Android Studio.

Right now I am building a WebView Android App and I want it to have pull to refresh feature to show new contents that just uploaded right away without having to close the app.

How to do that properly?

I have saw and tried so many tutorials and still I can not fully implement this feature.

At most, I only able to show the refresh icon but still not able to update the contents.

This is the codes I use in case you would kindly help solve this matter of mine.

activity_main xml

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

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

MainActivity Java

package com.chandragedanggoreng.bolanews;

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

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private WebView mywebView;
private SwipeRefreshLayout refreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    mywebView=(WebView) findViewById(R.id.webview);
    mywebView.setWebViewClient(new WebViewClient());
    mywebView.loadUrl("https://xxxx.xxx/");
    WebSettings webSettings=mywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh () {
            refreshLayout.setRefreshing(false);
        }
    });
}

@Override
public void onRefresh () {
}

public class mywebClient extends WebViewClient{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon){
        super.onPageStarted(view,url,favicon);
    }
        @Override
    public boolean shouldOverrideUrlLoading(WebView view,String url){
        view.loadUrl(url);
        return true;
    }
}
@Override
public void onBackPressed(){
    if(mywebView.canGoBack()) {
        mywebView.goBack();
    }
    else{
        super.onBackPressed();
    }
}
}

Thank you.

2 Answers2

1

Add this code in onCreate method

refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        refreshLayout.setRefreshing(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(false);mywebView.reload();
            }
        },2000);
    }
});

refreshLayout.setColorSchemeColors(
        getResources().getColor(android.R.color.holo_blue_dark),
        getResources().getColor(android.R.color.holo_orange_dark),
        getResources().getColor(android.R.color.holo_green_dark),
        getResources().getColor(android.R.color.holo_red_dark)


);
0

During my research, when we searched for a sustainable pull-to-refresh approach in Android WebView, I found out that there are three easy ways that you can implement pull to refresh in your Android WebView app:

  • You can use a third-party library / SDKs (Java or Kotlin)

OR

  • You can use your custom implementation (like with code snippets mentioned here). This option is more complicated but provides greater flexibility for customization (Java or Kotlin)

OR

  • In Android WebViewGold, within Config.java, turn ENABLE__PULL_REFRESH to true (this does not work in Kotlin)
Elikill58
  • 4,050
  • 24
  • 23
  • 45