ads_header

How To Implementation Swipe To Refresh Android Studio


Hello guys, in this post we will discuss how to implement Swipe To Refresh on Android.

Swipe To Refresh is the process of refreshing a page on Android by rubbing it vertically down on the page to be reloaded. In applying swipe to refresh on android, we will find it easier to see the state of the page we access.

This application is very popular among applications that have triumphed like Facebook, Instagram, Line and many more.





Okay, first make a new project first. When finished, open activity_main.xml and enter the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipenya"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Now, open MainActivity.java then enter the following code

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    SwipeRefreshLayout mSwipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipenya);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // do something
                        Toast.makeText(MainActivity.this, "do something", Toast.LENGTH_LONG).show();  
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
    }
}

In the Toast command, you can change with your own command.

When it's finished, the program is ready to run, and more or less the results as shown below

How To Implementation Swipe To Refresh Android Studio

Powered by Blogger.