HOW TO ADD SWIPE REFRESH IN ANDROID STUDIO - Andropro

Andropro

androidtutorialro

Breaking

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Wednesday, June 20, 2018

HOW TO ADD SWIPE REFRESH IN ANDROID STUDIO

HOW TO ADD SWIPE REFRESH LAYOUT IN WEBVIEW:

To add the swipe refresh whenever swiped follow the steps given below,swipe refresh in webview help to reload the webpage when there is error or page not loaded,adding an swipe refresh is easy and simple,your webview must be under the swipe refresh layout for the working of an swipe refreshing in android studio,

ADDING SWIPE TO REFRESH TO YOUR ANDROID PROJECT:

To add swipe to refresh to your project you need to define the swipe refresh layout above the webview inside the activity_main.xml


  <android.support.v4.widget.SwipeRefreshLayout   
.<webview>
.
.</webview>
.  </android.support.v4.widget.SwipeRefreshLayout   

Activity_main.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.support.v4.widget.SwipeRefreshLayout    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </WebView>
    </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>


</RelativeLayout>



MainActivity.java:


public class MainActivity extends Activity
{
    WebView browser;
    @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.webView);
        browser.setWebViewClient(new MyBrowser());
        browser.loadUrl("http://google.com");
        swipeView.setColorScheme(android.R.color.holo_blue_dark,android.R.color.holo_blue_light, android.R.color.holo_green_light,android.R.color.holo_green_dark);
        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(browser.getUrl());
                    }
                }, 4000);
            }
        });
    }
    private class MyBrowser extends WebViewClient  //If you click on any link inside the webpage of the WebView , that page will not be loaded inside your WebView. In order to do that you need to extend your class from WebViewClient by using the below function.    {
        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}


WATCH VIDEO TUTORIAL FOR HOW TO ADD SWIPE REFRESH ADDING TO AN ANDROID STUDIO






Post Bottom Ad

Responsive Ads Here

Pages