Is There a Better Way to Refresh Webview

Is there a better way to refresh WebView?

1) In case you want to reload the same URL:

mWebView.loadUrl("http://www.websitehere.php");

so the full code would be

newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dgeActivity.this.mWebView.loadUrl("http://www.websitehere.php");
}});

2) You can also call mWebView.reload() but be aware this reposts a page if the request was POST, so only works correctly with GET.

Is there a way to refresh webView every x seconds?

Easy to solve as long as the Activity is running in foreground:

 Handler handler = new Handler();
public void reloadWebView() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
webView.reload();
reloadWebView();
}
}, 5000);}

Note that handler is now a field, out of reloadWebView().
Now call it in your onCreate():

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reloadWebView();
}

Is there a way to refresh android WebView in background service?

make your Service bind to Activity and send message forcing WebView refresh or load your URL which may be passed through binder

you haven't posted single line of code so this is most precise answer you can get...

How to reload webview in Flutter?

First you have to obtain WebViewController and store it. To do it move the WebView to StatefulWidget (let's name it WebViewContainer) and pass onWebViewCreated callback to it.

Now you are able to reload WebView by calling webViewController.reload() method.

Second thing to do is to make reload trigger from outside. There are multiple ways to do it, I think the easiest option would be to use GlobalKey. So you need to create a final webViewKey = GlobalKey<WebViewContainerState>(). Pass the webViewKey to WebViewContainer constructor. After that you'll be able to access WebViewContainerState through webViewKey.currentState.

Example code:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

final webViewKey = GlobalKey<WebViewContainerState>();

class WebViewPage extends StatefulWidget {
@override
WebViewPageState createState() => WebViewPageState();
}

class WebViewPageState extends State<WebViewPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("WebView example"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
// using currentState with question mark to ensure it's not null
webViewKey.currentState?.reloadWebView();
},
)
],
),
body: WebViewContainer(key: webViewKey),
);
}
}

class WebViewContainer extends StatefulWidget {
WebViewContainer({Key key}) : super(key: key);

@override
WebViewContainerState createState() => WebViewContainerState();
}

class WebViewContainerState extends State<WebViewContainer> {
WebViewController _webViewController;

@override
Widget build(BuildContext context) {
return WebView(
onWebViewCreated: (controller) {
_webViewController = controller;
},
initialUrl: "https://stackoverflow.com",
);
}

void reloadWebView() {
_webViewController?.reload();
}
}

( Android ) Webview refresh button

In Activity class :

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;

import static com.d4rkunicorn.partyhard.R.id.webView;

public class MainActivity extends ActionBarActivity {

WebView webview;

@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.button);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

webview.reload();

}
});
}

}

In Layout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/webview.xml">

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"
android:id="@+id/button" />

</RelativeLayout>

Adding pull to refresh on webview for refreshing

You could wrap webview in Swipe refesh layout like this

<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />

</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

In java

package com.vvhvb.hesselfeenstra.vvheerenveenseboys;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
WebView view;
SwipeRefreshLayout mySwipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
String url ="http://heerenveenseboys.nl/";
view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient());
view.loadUrl(url);

mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
view.reload();
}
}
);

}

}

Pull to Refresh on Webview for Updating Content

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)

);


Related Topics



Leave a reply



Submit