I implemented swipe to refresh for my WebView and its working fine. But there is one problem which i am not able to solve. The problem is my Swipe to Refresh hides after 6 seconds. It did not remain there till the loading for WebView is completed. What i want is that Swipe to refresh should remain visible till the page fully loads.
My Implementations
swipeView = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
myWebView = (WebView) view.findViewById(R.id.webview);
myWebView.loadUrl("http://m.facebook.com");
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
#Override
public void onRefresh()
{
swipeView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable()
{
#Override
public void run()
{
swipeView.setRefreshing(false);
myWebView.loadUrl("http://m.facebook.com");
}
}, 6000);
}});
Use WebView listener and finish your Swipe in the onPageFinished listener..
For example like below
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
swipeView.setRefreshing(false);
}
});
For your case, change your code like below,
swipeView = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
myWebView = (WebView) view.findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
swipeView.setRefreshing(false);
}
});
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
#Override
public void onRefresh()
{
myWebView.loadUrl("http://m.facebook.com");
}});
Related
I am trying to implement a progress bar at the top of my web-view while the page loads. Just like chrome.
How can i do this?
i have a swipe refresh view in a container.
I've looked at a lot of answers here and tried to implement them, but had no luck.
I just want to implement a simple progress bar (like chrome) every time the web-view loads.
public class MainActivity extends Activity {
WebView browser;
SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.webview);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// open in Webview
if (url.contains("example.com") ){
// Can be clever about it like so where myshost is defined in your strings file
// if (Uri.parse(url).getHost().equals(getString(R.string.myhost)))
return false;
}
// open rest of URLS in default browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
public void onPageFinished(WebView view, String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
// Load the webpage
browser.loadUrl("https://example.com/");
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
browser.reload();
}
}
);
}
I developed a website for my entreprisee and I work almost exclusively with PHP
So the Java language (and android studio) is a really new for me
Despite this I have to create an APK to use the website (in order to block the android home on this site)
I would like to know how to generate errors in order to test this function
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
LoadWeb();
progressBar.setMax(100);
progressBar.setProgress(1);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
//Don't show ProgressBar
progressBar.setVisibility(View.INVISIBLE);
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
public void LoadWeb() {
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("mysite.com");
swipe.setRefreshing(true);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
}
One way of doing this could be by adding a proxy(using app like fiddler) which returns custom error code as per your wish for all requests from your device.
In case you do not need specific error codes, just use a random system wide wifi proxy which actually does not exist. In this case, device will try sending data to this proxy but won't find it and you will get a callback in onReceivedError.
I am asking for help, the problem is the refresh icon disappears before the page is fully loaded.
Here is the code and thanks in advance:
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},150000);
The 150000 is the amount of time it keeps appearing on the screen , I tried removing it but the the following override codes was messed up.I am a beginner so sorry if the solution is simple .
As far as I understand your question, you want to refresh the web-page via SwipeRefreshLayout and show loading until whole page finish up loading.
For that remove this
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},150000);
you are stop loading page after 15 secs by yourself this is not necessary.
Instead do the following
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
Note : Set the WebViewClient to know a event when page finished it's loading.
Hope this works for you!
I need to pull from the top to refresh my web view
I found this on Android Developer site but i don't know how to use it
xml Code
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java code
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myUpdateOperation();
}
}
);
You gotta put your WebView inside SwipeRefreshLayout:
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
String currentUrl = "https://news.ycombinator.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
webView.loadUrl(currentUrl);
webView.setWebViewClient(new MyWebViewClient());
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.loadUrl(currentUrl);
}
});
}
public class MyWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
}
}
swipeRefreshLayout.setRefreshing(false) stops the animation.
To be able to refresh the page with the same URL running
you gotta save your link in ISharedPreferences when page first loaded
public override void OnPageFinished(WebView view, string url)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("MyURL", url);
editor.Commit();
}
when you load the URL for reffresh use the saved URL
string SaveURL = prefs.GetString("MyURL", "");
webView.loadUrl(SaveURL);
-- Other Solution is
Webview.Reload(); // This code Refreshes the current page loaded
I am trying to using Webview and also to pull down to refresh the CURRENT page. Everything going good except this one.
The Problem is to Refresh the current page.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe);
browser = (WebView) findViewById(R.id.webView1);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl("https://www.google.co.in/");
swipeView.setColorSchemeColors(R.color.blue, R.color.purple, R.color.green, R.color.orange);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeView.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
swipeView.setRefreshing(false);
browser.loadUrl("https://www.google.co.in/");
}
}, 4000);
}
});
}
private class MyBrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
as per my code Refreshing is working but when i move to some links, to refresh means the page redirect to google.com. Current page not refreshing it will redirected another URL.