You might have noticed the shimmer effect on Facebook’s mobile app while the data is loading from the network. Shimmer library was created by Facebook to display an animation when data is loading to make the UI more interesting and beautiful,
instead of using the traditional ProgressBar. Facebook, later on, released an open-source library called Shimmer which we can use to implement the Shimmer Effect Placeholder.
Shimmer for Android is implemented as a layout, which means that you can simply nest any view inside a ShimmerFrameLayout.
Step by Step Implementation
Step 1: Add dependency of Shimmer Effect library in build.gradle file
hen Navigate to gradle scripts and then to build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.
implementation ‘com.facebook.shimmer:shimmer:0.5.0’
now click on Sync it will sync your all files in build.gradle().
Step 2: Create a new Shimmer Effect in your activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<!--Shimmer Effect-->
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmer_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<!--Image-->
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/ic_baseline_account_balance_24" />
<!--Text given to Image-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Bank"
android:textColor="@color/purple_200"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
<!--Button1 to start Shimmer Effect-->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:paddingLeft="20dp"
android:text="Start" />
<!--Button2 to stop Shimmer Effect-->
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:paddingRight="20dp"
android:text="Stop" />
</RelativeLayout>
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.shimmer.ShimmerFrameLayout;
public class MainActivity extends AppCompatActivity {
// Variables created for buttons and Shimmer
Button button1, button2;
ShimmerFrameLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
// Button 1 to start Shimmer Effect
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If auto-start is set to false
container.startShimmer();
}
});
// Button 2 to stop Shimmer Effect
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If auto-start is set to false
container.stopShimmer();
}
});
// Shimmer effect
container = (ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
}
}
Now click on the run option it will take some time to build Gradle. After that, you will get output on your device.
Ta-da!! That’s all. Keep Coding!!