Improve user experience by adding your own custom loading animation, this blog post talks about creating a simple loading animation.
##Introduction Progress bars let the user know that the device is busy, and in particular the indeterminate mode the progress bar shows a cyclic or a horizontal animation without an indication of progress. we can always improve this experience by adding our own custom loading screen, to see how this works lets take example of this sample application(Let’s call it taxi sure, which helps you rent taxi) that has a simple splash screen activity. first let’s see how the application splash screens looks with the oridnary dull indeterminate progress(circular).
Now let’s try some custom indeterminate progress view, let’s assume app is about renting cars
. Hence i will use car icons for loading icons.
##Implementation part: Create a class called CarLoadingView
which extends Linear Layout, now define two constructors one is to create view through xml and the latter one is programmatically in java.
and
now let’s consider the first constructor, which gets called during xml inflation.
please note that first,second,third and fourth are all global imageview variables.
One thing to note is that the constructor inflates a xml layout called car_loading_row
. Now this xml consists of a Linear Layout with orientation set horizontally and 4 Imageviews as childviews.
we also instantiate a handler and define a runnable as shown in the constructor, at first we manually call run() method
of the runnable positionswitcher, later on we use handler to do the job for every 250ms. By default all the imageviews are set with darker background images. If you haven’t noticed there are 2 images with color code #cbcbcb and #fff, were #fff is highlighted image. The logic here is we increment a int variable called position in the runnable and call a method called switch position were we set image view with the corresponding images. We do modulo of the position passed so that we can limit it upto 3(0-3) positions
only.
Please note that inside runnable i use the handler object that was instantiated earlier in the constructor. and last but not the least don’t forget to add custom view in the activity layout file.
More Reading:
you can find the source code here.