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!
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 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.
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");
}});
I use a SwipeRefreshLayout to reload the content of a listview.
It works and the onRefreshListener is triggered but the small loading spinner that appears onswipe doesn't want to dismiss after the loading is complete.
Is there a way to make it go ?
Edit : fixed SwipeView means SwipeRefreshLayout
Do you mean SwipeRefreshLayout? if so use SwipeRefreshLayout.setRefreshing(false)
The correct way of using it is like this:
if (swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
If you are using SwipeRefreshLayout then use swipeLayout_object.setRefreshing(false); for dismiss that loading icon. i.e.
private SwipeRefreshLayout swipeLayout;
protected void onCreate(Bundle savedInstanceState) {
....
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
//Do your task
swipeLayout.setRefreshing(false);
}
});
}
Details available here.
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
For Kotlin,
SwipeRefreshLayout.isRefreshing = false
here is complete solution for loading a page and remove loading animation after page is fully loaded.
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
mWebView = (WebView) findViewById(R.id.htmlview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(),
"demo"); mWebView.loadUrl("file:///android_asset/demo.html");
i know how to invoke a page's javascript,but if the page have no the js i need.if i can invoke a js that i write?
webview of android;
thanks
Haven't done this before, but this looks good for me: How to append additional text with existing html content in android?
It says, after loading the page into your WebView, you can just execture JavaScript by calling
myWebView.loadUrl("javascript:alert('My JavaScript Call')");
So many way to call javascript in your web view
myWebView.loadUrl("javascript:testEcho('Hello World!')");
or
public void run(final String scriptSrc) {
webView.post(new Runnable() {
#Override
public void run() {
webView.loadUrl("javascript:" + scriptSrc);
}
});
}
or
public void run(final String scriptSrc) {
webView.post(new Runnable() {
#Override
public void run() {
webView.loadUrl("javascript:" + scriptSrc);
}
});
}
this question to help me achieve this task for me.
You can also use WebViewClient for call java script.
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished (WebView webView, String url)
{
webView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';");
}
});
for more help u can also see this link .