Advanced Android - loader



Getting data from server may take few seconds during which we can show loader/progress to the user instead of blank activity/page

Example

This example demostrate about how to integrate Android loader.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   android:padding = "16dp">
   <RelativeLayout
      android:id = "@+id/first_row"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:orientation = "horizontal"
      android:padding = "20dp">
      <com.wang.avi.AVLoadingIndicatorView
         android:id = "@+id/material_design_ball_pulse_loader_progress"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:visibility = "visible"
         app:indicator = "BallPulse"
         app:indicator_color = "#ff00" />
      <com.wang.avi.AVLoadingIndicatorView
         android:id = "@+id/material_design_ball_clip_rotate_multiple_loader"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignParentRight = "true"
         android:visibility = "visible"
         app:indicator = "BallClipRotateMultiple"
         app:indicator_color = "#ff00" />
   </RelativeLayout>
   <RelativeLayout
      android:id = "@+id/second_row"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_below = "@+id/first_row"
      android:layout_marginTop = "20dp"
      android:orientation = "horizontal"
      android:padding = "20dp">
      <com.wang.avi.AVLoadingIndicatorView
         android:id = "@+id/material_design_linear_scale_progress_loader"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_centerInParent = "true"
         android:visibility = "visible"
         app:indicator = "LineScale"
         app:indicator_color = "#ff00" />
   </RelativeLayout>
   <RelativeLayout
      android:id = "@+id/third_row"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_below = "@+id/second_row"
      android:layout_marginTop = "20dp"
      android:orientation = "horizontal"
      android:padding = "20dp">
      <com.wang.avi.AVLoadingIndicatorView
         android:id = "@+id/material_design_ball_rotate_loader"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:visibility = "visible"
         app:indicator = "BallRotate"
         app:indicator_color = "#ff00" />
      <com.wang.avi.AVLoadingIndicatorView
         android:id = "@+id/material_design_linear_spin_fade_loader"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignParentRight = "true"
         android:visibility = "visible"
         app:indicator = "LineSpinFadeLoader"
         app:indicator_color = "#ff00" />
   </RelativeLayout>
   <com.wang.avi.AVLoadingIndicatorView
      android:id = "@+id/material_design_ball_grid_beat_loader"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_below = "@+id/third_row"
      android:layout_centerInParent = "true"
      android:layout_gravity = "center_horizontal"
      android:layout_marginTop = "20dp"
      android:visibility = "visible"
      app:indicator = "BallGridBeat"
      app:indicator_color = "#ff00" />
   <com.wang.avi.AVLoadingIndicatorView
      android:id = "@+id/material_design_ball_spin_fade_loader"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_below = "@+id/material_design_ball_grid_beat_loader"
      android:layout_marginTop = "20dp"
      android:visibility = "visible"
      app:indicator = "BallSpinFadeLoader"
      app:indicator_color = "#ff00" />
   <com.wang.avi.AVLoadingIndicatorView
      android:id = "@+id/material_design_ball_scale_ripple_loader"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_alignParentRight = "true"
      android:layout_below = "@+id/material_design_ball_grid_beat_loader"
      android:layout_marginTop = "20dp"
      android:visibility = "visible"
      app:indicator = "BallScaleRipple"
      app:indicator_color = "#ff00" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      ballPulseAnimLoader();
      ballClipRotateMultipleAnimateLoader();
      lineScaleLoaderExample();
      ballRotateLoaderAndroidExample();
      lineSpinFadeLoaderLoader();
      androidBallGridBeatLoaderBar();
      androidBallSpinFadeLoaderProgressBar();
      ballScaleRippleProgressLoaderExample();
   } 
   void ballPulseAnimLoader() {
      findViewById(R.id.material_design_ball_pulse_loader_progress).setVisibility(View.VISIBLE); 
   } 
   void ballClipRotateMultipleAnimateLoader() {
      findViewById(R.id.material_design_ball_clip_rotate_multiple_loader).setVisibility(View.VISIBLE);
   } 
   void lineScaleLoaderExample() {
      findViewById(R.id.material_design_linear_scale_progress_loader).setVisibility(View.VISIBLE); 
   } 
   void ballRotateLoaderAndroidExample() {
      findViewById(R.id.material_design_ball_rotate_loader).setVisibility(View.VISIBLE);
   } 
   void lineSpinFadeLoaderLoader() {
      findViewById(R.id.material_design_linear_spin_fade_loader).setVisibility(View.VISIBLE);
   } 
   void androidBallGridBeatLoaderBar() { 
      findViewById(R.id.material_design_ball_grid_beat_loader).setVisibility(View.VISIBLE);
   } 
   void androidBallSpinFadeLoaderProgressBar() {
      findViewById(R.id.material_design_ball_spin_fade_loader).setVisibility(View.VISIBLE); 
   } 
   void ballScaleRippleProgressLoaderExample() {
      findViewById(R.id.material_design_ball_scale_ripple_loader).setVisibility(View.VISIBLE); 
   }
}

Step 4 − Add the following code to build.gradle

dependencies { 
   compile fileTree(include: ['*.jar'], dir: 'libs')
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.1'
   compile 'com.android.support:design:23.1.0'
   compile 'com.wang.avi:library:1.0.3'
   compile 'com.nineoldandroids:library:2.4.0'
}

Step 5 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Loaders
Advertisements