SwipeRefreshLayout example in android

Why to use SwipeRefreshLayout

As a developer you need to understand the need of the user's and their behaviors also, now a days users are familiar with tap and swipe down, this made them easier to do work and everything. So as per the change of user's behavior as a developer you need to make your app familiar for them, That's the reason why you should use SwipeRefreshLayout.

Here Is an example for the SwipeRefreshLayout

This is for androidx, put the below code in your android xml layout file.

1
2
3
4
5
6
7
8
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/appList_pulldown"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<!-- Your Recycler view or other view here-->
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Above Code is for the layout and pur below java code into your Activity.java file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
SwipeRefreshLayout appList_pulldown;

//initialize SwipeRefreshLayout
appList_pulldown = findViewById(R.id.appList_pulldown);

//setOnRefreshListener 
appList_pulldown.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //Your Code to do when swipe...
            }
        });

//finally set SwipeRefreshLayout to refreshing false.

appList_pulldown.setRefreshing(false);

Result is Here in the snapshot:
That's It, The only code above will make your user feel better for your apps performance.

Happy Coding!
Thanks.

Comments

Post a Comment