Pull to Refreshing webview - android

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.

Related

How can i add a horizontal progress bar on top of web-view?

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();
}
}
);
}

How to get url after loading page in webview

I need to take url after loading a web page in my web view to check condition and if I use onPageFinished() method, then where to use.
Just set a webview client and override onpagefinished as shown below
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
}
});
}
}
inside onpagefinished you have the url

How to add SwipeRefreshLayout to WebView

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

Swipe to Refresh hides before loading the WebView

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");
}});

android webview probleme loading video

I am creating an android app.
Webview with a webpage including videos.
I am not able to start these videos this is my Main:
public class MainActivity extends ActionBarActivity {
#SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.___.com/";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setSupportZoom(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.out.println("hello");
return false;
}
});
view.loadUrl(url);
}
When I start a video it remains loading but never shows content.

Categories

Resources