Bart Friederichs
Bart Friederichs

Reputation: 33573

How to implement indeterminate loading screen

I am building an app that uses a REST interface to fetch data. Usually, I would call setProgressBarIndeterminateVisibility(true); to let the user know the app is waiting for data. However, this seems to be deprecated:

Progress bars are no longer provided in AppCompat.

When looking at the GMail app, I see there is an indeterminate progressbar (loading icon) full screen, where the data is going to come.

How would I implement that correctly? Some of activities are extended from ListViewActivity and use the setEmptyView to show a simple TextView when no data is available. Some are more elaborate layouts.

I could think of using setVisibility(View.GONE), but wondering if this is the "right" way to do it.

Upvotes: 0

Views: 215

Answers (2)

Alexandr Shutko
Alexandr Shutko

Reputation: 1875

Wrap your content layout into FrameLayout. Add to FrameLayout something like this:

<LinearLayout
    android:id="@+id/loading_progress"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="invisible">

    <ProgressBar
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:indeterminate="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Loading data..."/>
</LinearLayout>

And then switch content / progress layers in code using .setVisibility(View.VISIBLE) / .setVisibility(View.INVISIBLE)

Upvotes: 0

Budius
Budius

Reputation: 39856

Yes. That is the right way. Just layout everything where they should be on the XML and call setVisibility on your progressBar when necessary.

There're usually two ways of kwnoing "when" to call.

  1. on your REST callbacks (e.g. onSuccess(), onStartLoading())
  2. using a DataSetObserver on your adapter (which is exactly what the ListView does). Like the following code:

.

@Override
protected void onStart(){
   super.onStart()
   observer.onChanged();
   adapter.registerDataSetObserver(observer);
}

@Override
protected void onStop(){
   super.onStop()
   adapter.unregisterDataSetObserver(observer);
}

private DataSetObserver observer = new DataSetObserver(){
   @Override
   public void onChanged() {
       progress.setVisibility(adapter.getCount()==0?View.VISIBLE:View.GONE);
   }
}

Upvotes: 1

Related Questions