I am currently using in my application a listview that need maybe one second to be displayed.
What I currently do is using the @id/android:empty property of the listview to create a "loading" text.
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Loading..."/>
Now, I would like to replace that with the animated circle that is used in a loading dialog instead of this text, i guess you all know what I mean:
Edit: I do not want a dialog. I want to show that inside my layout.
Thank a lot for your help!
You can use this code from firebase github samples ..
You don't need to edit in layout files ... just make a new class "BaseActivity"
In your Activity that you want to use the progress dialog ..
Before/After the function that take time
You can do this by using the following xml
With this style
To use this, you must hide your UI elements by setting the visibility value to GONE and whenever the data is loaded, call
setVisibility(View.VISIBLE)
on all your views to restore them. Don't forget to callfindViewById(R.id.loadingPanel).setVisiblity(View.GONE)
to hide the loading animation.If you dont have a loading event/function but just want the loading panel to disappear after x seconds use a Handle to trigger the hiding/showing.
This is generally referred to as an Indeterminate Progress Bar or Indeterminate Progress Dialog.
Combine this with a Thread and a Handler to get exactly what you want. There are a number of examples on how to do this via Google or right here on SO. I would highly recommend spending the time to learn how to use this combination of classes to perform a task like this. It is incredibly useful across many types of applications and will give you a great insight into how Threads and Handlers can work together.
I'll get you started on how this works:
The loading event starts the dialog:
Now the thread does the work:
Finally get the state back from the thread when it is complete:
If you would like to not inflate another view just to indicate progress then do the following:
Android will take care the progress bar's visibility.
For example, in
activity_main.xml
:And in
MainActivity.java
:}
For the ones developing in Kotlin, there is a sweet method provided by the Anko library that makes the process of displaying a
ProgressDialog
a breeze!Based on that link:
This will show a Progress Dialog with the progress % displayed (for which you have to pass the
init
parameter also to calculate the progress).There is also the
indeterminateProgressDialog()
method, which provides the Spinning Circle animation indefinitely until dismissed:Shout out to this blog which led me to this solution.
Simply put this block of xml in your activity layout file:
And when you finish loading, call this one line:
The result (and it spins too):