11

I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.

A progress view is shown when user swipes down in order to reload data from server. The progress view looks like a piece of circle that grows and shrinks during the animation. It seems that the style of this progress view cannot be customized much. However, I am fine with its built-in style.

I also show the same progress animation during initial data loading. This can be achieved by calling mySwipeRefreshLayout.setRefreshing(true). That's perfectly OK too.

However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.

Is there a simple and clean way to have the same progress indicator for both a SwipeRefreshLayout and a form activity that does not contain any list view and refresh layout and does not support any swipe gesture?

Thanks.

Cimlman
  • 3,404
  • 5
  • 25
  • 35

2 Answers2

10

However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.

yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.

enter image description here

i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <SeekBar
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="54dp" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>


</LinearLayout>

now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);


        mSwipeRefreshLayout.setOnRefreshListener(null);

        ((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // mSwipeRefreshLayout.setRefreshing(true);
                // call AsyncTask
                new LongOperation().execute("");
            }
        });

    }

    private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            // stop mSwipeRefreshLayout
            mSwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        protected void onPreExecute() {
            // start mSwipeRefreshLayout
            mSwipeRefreshLayout.setRefreshing(true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

EDITED:15/02/20 After 4 year, Here is great explanation on android developer site

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • Thanks for complex example (+1). I must admit that I was too lazy to experiment with this approach myself. I would have to wrap all my activities by `SwipeRefreshLayout` just to "steal" its progress animation. I consider this to be a "dirty" solution. I hoped that there could possibly be e.g. a way to set some style to `ProgressBar` so that it looks like similarly to progress view of `SwipeRefreshLayout`. – Cimlman Feb 09 '16 at 17:13
  • Not sure I would consider this a dirty solution, it was easy to implement for a webview and looks way better than the standard progressbars. Thanks! – Mike Purcell Oct 20 '16 at 14:46
  • It also important to desable the SwipeRefreshLayout after the command mSwipeRefreshLayout.setRefreshing(false) with mSwipeRefreshLayout.setEnable(false). In order to desactivate the animation when swipe. Of course you have to reactivate the SwipeRefreshLayout before calling mSwipeRefreshLayout.setRefreshing(true) again. – Samuel L Feb 25 '17 at 07:36
2
mSwipeRefreshLayout.setOnRefreshListener(null) 

didn't work for me, so I made this method to enable and disable refreshing.

 private void setToRefresh(boolean refresh) {
    if (refresh) {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setRefreshing(true);
    } else {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setEnabled(false);
    }
}
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65