Introduction
The shimmer is a library that provides the shimmer effect with the help of the ShimmerLayout class. It is developed by Facebook. The shimmer effect can be added to a single view or an entire layout.
Nowadays, progress bars in Android are not so attractive and do not make the app's layout great. Well, Shimmer solves this problem by providing a great animation of the shimmer effect. Modern applications mostly use the shimmer effect to show their lists.
In this article, we will develop an application that shows the loading of a list with the shimmer effect, not with a progress bar.
Step 1
Add Shimmer Gradle to your build.gradle.
- apply plugin: 'com.android.application'
-
- android {
- compileSdkVersion 28
- defaultConfig {
- applicationId "yourdomain.shimmereffectsample"
- minSdkVersion 21
- targetSdkVersion 28
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
-
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:28.0.0'
- implementation 'com.android.support.constraint:constraint-layout:1.1.3'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'com.android.support.test:runner:1.0.2'
- androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
-
- implementation 'com.facebook.shimmer:shimmer:0.4.0'
-
- implementation 'com.android.support:recyclerview-v7:28.0.0'
- }
After adding shimmer and recyclerview to the build.gradle, now add recyclerview to
activity_main.xml.
Step 2
Adding recyclerview and ShimmerLayout to
activity_main.xml -
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <com.facebook.shimmer.ShimmerFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/shimmer_view_container"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:orientation="vertical"
- android:background="#ffffff">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <include layout="@layout/list_shimmer_layout" />
- <include layout="@layout/list_shimmer_layout" />
- <include layout="@layout/list_shimmer_layout" />
- <include layout="@layout/list_shimmer_layout" />
- <include layout="@layout/list_shimmer_layout" />
- <include layout="@layout/list_shimmer_layout" />
- </LinearLayout>
- </com.facebook.shimmer.ShimmerFrameLayout>
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recycler_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
-
- </RelativeLayout>
Now, we can see that inside the ShimmerFrameLayout tag, a layout named list_shimmer_layout is added. So, it's time to code this layout.
Step 3
Add Views to
list_shimmer_layout.xml .
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main_view"
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:paddingLeft="10dp"
- android:paddingRight="10dp">
-
- <ImageView
- android:id="@+id/user_image_View"
- android:layout_width="45dp"
- android:layout_height="45dp"
- android:layout_centerVertical="true"
- android:background="#dddddd"
- android:scaleType="center" />
-
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_toEndOf="@id/user_image_View"
- android:padding="10dp">
-
- <TextView
- android:id="@+id/discount_text_view"
- android:layout_width="match_parent"
- android:layout_height="18dp"
- android:background="#dddddd" />
-
- <TextView
-
- android:id="@+id/categories_heading_text_view"
- android:layout_width="150dp"
- android:layout_height="16dp"
- android:layout_below="@id/discount_text_view"
- android:layout_marginTop="4dp"
- android:background="#dddddd"
- android:paddingRight="10dp" />
-
- <TextView
- android:id="@+id/categories_details_text_view"
- android:layout_width="200dp"
- android:layout_height="16dp"
- android:layout_below="@id/categories_heading_text_view"
- android:layout_marginTop="5dp"
- android:background="#dddddd"
- android:ellipsize="end"
- android:maxLines="2"
-
- />
- </RelativeLayout>
-
-
- </RelativeLayout>
Step 4
Since we are showing the data and Shimmer effect on a list, let's create a list item layout.
Add Views to
list_items.xml.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/main_view"
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:paddingLeft="10dp"
- android:paddingRight="10dp">
-
- <ImageView
- android:id="@+id/user_image_View"
- android:layout_width="45dp"
- android:layout_height="45dp"
- android:layout_centerVertical="true"
- android:src="@drawable/ic_user" />
-
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_toEndOf="@id/user_image_View"
- android:padding="10dp">
-
- <TextView
- android:id="@+id/user_name_textview"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- tools:text="Gaurav Kumar"
- android:textSize="15sp"/>
-
- <TextView
-
- android:id="@+id/user_profession"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/user_name_textview"
- android:layout_marginTop="2dp"
- tools:text="Software Engineer"
- android:textSize="13sp"/>
-
- <TextView
- android:id="@+id/user_address"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/user_profession"
- android:layout_marginTop="2dp"
- android:textSize="13sp"
- tools:text="Noida"
-
- />
- </RelativeLayout>
-
-
- </RelativeLayout>
Step 4
Here, we need a pojo class named
UserDataModel.java.
- public class UserDataModel {
-
- String name;
- String profession;
- String city;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getProfession() {
- return profession;
- }
-
- public void setProfession(String profession) {
- this.profession = profession;
- }
-
- public String getCity() {
- return city;
- }
-
- public void setCity(String city) {
- this.city = city;
- }
-
-
- }
Step 4
We need an adapter to show the data in the list. Let's see the adapter's code mentioned below. We have named it
UserAdapter.java.
- import android.support.annotation.NonNull;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- import java.util.ArrayList;
-
- public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
-
- private ArrayList<UserDataModel> mlist;
-
- public UserAdapter(ArrayList<UserDataModel> list) {
-
- mlist = list;
-
- }
-
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
- View itemView = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.list_items, parent, false);
-
- return new ViewHolder(itemView);
- }
-
- @Override
- public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
-
- viewHolder.nameTextView.setText(mlist.get(i).getName());
- viewHolder.professionTextView.setText(mlist.get(i).getProfession());
- viewHolder.cityTextView.setText(mlist.get(i).getCity());
-
- }
-
- @Override
- public int getItemCount() {
- return mlist.size();
- }
-
- public class ViewHolder extends RecyclerView.ViewHolder {
-
- TextView nameTextView;
- TextView professionTextView;
- TextView cityTextView;
-
- public ViewHolder(View itemView) {
- super(itemView);
-
- nameTextView = itemView.findViewById(R.id.user_name_textview);
- professionTextView = itemView.findViewById(R.id.user_profession);
- cityTextView = itemView.findViewById(R.id.user_address);
-
- }
-
-
- }
-
- }
Step 5
Now, bind all the Views in
MainActivity.java and initialize these Views. Here comes the java activity.
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.DefaultItemAnimator;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
-
- import com.facebook.shimmer.ShimmerFrameLayout;
-
- import java.util.ArrayList;
-
- public class MainActivity extends AppCompatActivity {
-
-
- ShimmerFrameLayout shimmerLayout;
- RecyclerView mRecyclerView;
- ArrayList<UserDataModel> mUserArrayList;
- UserAdapter mAdapter;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- shimmerLayout = findViewById(R.id.shimmer_view_container);
- mRecyclerView = findViewById(R.id.recycler_view);
-
- mUserArrayList = new ArrayList<>();
-
- showShimmer();
-
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- populateList();
- mAdapter = new UserAdapter(mUserArrayList);
- RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- mRecyclerView.setAdapter(mAdapter);
- hideShimmer();
- }
- }, 3000);
-
-
- s
- }
-
- private void populateList() {
-
- UserDataModel dataModel = new UserDataModel();
- dataModel.setName("Gaurav Kumar");
- dataModel.setProfession("Android Developer");
- dataModel.setCity("Noida");
- mUserArrayList.add(dataModel);
-
- UserDataModel dataModel1 = new UserDataModel();
- dataModel1.setName("Ashish Tiwari");
- dataModel1.setProfession("Unity Game Developer");
- dataModel1.setCity("Delhi");
- mUserArrayList.add(dataModel1);
-
- UserDataModel dataModel2 = new UserDataModel();
- dataModel2.setName("Pravesh Dubey");
- dataModel2.setProfession("Ios Developer");
- dataModel2.setCity("Pune");
- mUserArrayList.add(dataModel2);
-
- UserDataModel dataModel3 = new UserDataModel();
- dataModel3.setName("Praveen Singh togadia");
- dataModel3.setProfession("Web Developer");
- dataModel3.setCity("Gurgaon");
- mUserArrayList.add(dataModel3);
-
- UserDataModel dataModel4 = new UserDataModel();
- dataModel4.setName("Narendra singh");
- dataModel4.setProfession("Sql Developer");
- dataModel4.setCity("Banglore");
- mUserArrayList.add(dataModel4);
-
-
- }
-
- private void hideShimmer() {
- shimmerLayout.stopShimmer();
- shimmerLayout.setVisibility(View.GONE);
- }
-
- private void showShimmer() {
- shimmerLayout.startShimmer();
- shimmerLayout.setVisibility(View.VISIBLE);
- }
- }
See, this is so simple to implement. Here, we are delaying the setting of the adapter because we are not making any API call. So, the effect can't be seen without delaying the output. Let's look at the two methods - showShimmer() and hideShimmer(). These two methods are responsible for showing and hiding the shimmer effect respectively.
Output
There are several images to show the effect, so please see carefully the stages and the difference in images.
This is the initial stage of the shimmer. Now, let us see other images to mark a difference.
See the right side is darker and the left side of Views is lighter in color. This indicates that shimmer effect travels left to right. See another image for a clear explanation.
Now, after that effect, the data will load as below.
Conclusion
In this article, we learned about the shimmer effect with the help of various images. We saw the stages of shimmer layout with an animated wave traveling from left to right. Finally, our data loaded and the shimmer effect got hidden and stopped. I hope this article was helpful to you.