Introduction

Ninety percent of people use Android smartphones. RecyclerView provides an efficient user interface design. A maximum number of multimedia and commercial Applications are made up of RecyclerView. Thus, I will show you how to create RecyclerView in an Android, using an Android Studio. Android is a kernel-based operating system. It allows the user to modify the GUI components and the source code.
Requirements

Steps to be followed are given below

Carefully follow my steps to create RecyclerView in an Android, using an Android Studio and I have included the source code given below.
Step 1
Open an Android Studio. Start the new project.
android
Step 2
Put the Application name and the company domain. If you wish to use C++ to code the project, mark the Include C++ support, followed by clicking Next.
android
Step 3
Select Android minimum SDK. Afterward, you need to choose the minimum SDK. It will show an approximate percentage of people. Use SDK, followed by clicking Next.
android
Step 4
Choose the basic activity, followed by clicking Next.
android
Step 5
Put the activity name and the layout name. Android Studio basically takes Java class name as what you provide for the activity name. My activity name is the default name.
Step 6
Go to activity_main.xml, followed by clicking the text bottom. This XML file contains the designing the code for an Android app. In activity_main.xml, copy and paste the code given below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/main_content"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@android:color/white"
  8. android:fitsSystemWindows="true">
  9. <android.support.design.widget.AppBarLayout
  10. android:id="@+id/appbar"
  11. android:layout_width="match_parent"
  12. android:layout_height="@dimen/detail_backdrop_height"
  13. android:fitsSystemWindows="true"
  14. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  15. <android.support.design.widget.CollapsingToolbarLayout
  16. android:id="@+id/collapsing_toolbar"
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:fitsSystemWindows="true"
  20. app:contentScrim="?attr/colorPrimary"
  21. app:expandedTitleMarginEnd="64dp"
  22. app:expandedTitleMarginStart="48dp"
  23. app:expandedTitleTextAppearance="@android:color/transparent"
  24. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  25. <RelativeLayout
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content">
  28. <ImageView
  29. android:id="@+id/backdrop"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent"
  32. android:fitsSystemWindows="true"
  33. android:scaleType="centerCrop"
  34. app:layout_collapseMode="parallax" />
  35. <LinearLayout
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_centerInParent="true"
  39. android:gravity="center_horizontal"
  40. android:orientation="vertical">
  41. <TextView
  42. android:id="@+id/love_music"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="@string/backdrop_title"
  46. android:textColor="@android:color/white"
  47. android:textSize="@dimen/backdrop_title" />
  48. <TextView
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:text="@string/backdrop_subtitle"
  52. android:textColor="@android:color/white"
  53. android:textSize="@dimen/backdrop_subtitle" />
  54. </LinearLayout>
  55. </RelativeLayout>
  56. <android.support.v7.widget.Toolbar
  57. android:id="@+id/toolbar"
  58. android:layout_width="match_parent"
  59. android:layout_height="?attr/actionBarSize"
  60. app:layout_collapseMode="pin"
  61. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  62. </android.support.design.widget.CollapsingToolbarLayout>
  63. </android.support.design.widget.AppBarLayout>
  64. <include layout="@layout/content_main" />
  65. </android.support.design.widget.CoordinatorLayout>
UI design of the code given above
android
Step 7
Create album_card.xml into the layout (app->res->layout). This XML file contains the designing code for an Android app. In album_card.xml, copy and paste the code given below.
album_card.xml code
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <android.support.v7.widget.CardView
  6. android:id="@+id/card_view"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:layout_gravity="center"
  10. android:layout_margin="@dimen/card_margin"
  11. android:elevation="3dp"
  12. card_view:cardCornerRadius="@dimen/card_album_radius">
  13. <RelativeLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent">
  16. <ImageView
  17. android:id="@+id/thumbnail"
  18. android:layout_width="match_parent"
  19. android:layout_height="@dimen/album_cover_height"
  20. android:background="?attr/selectableItemBackgroundBorderless"
  21. android:clickable="true"
  22. android:scaleType="fitXY" />
  23. <TextView
  24. android:id="@+id/title"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_below="@id/thumbnail"
  28. android:paddingLeft="@dimen/album_title_padding"
  29. android:paddingRight="@dimen/album_title_padding"
  30. android:paddingTop="@dimen/album_title_padding"
  31. android:textColor="@color/album_title"
  32. android:textSize="@dimen/album_title" />
  33. <TextView
  34. android:id="@+id/count"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@id/title"
  38. android:paddingBottom="@dimen/songs_count_padding_bottom"
  39. android:paddingLeft="@dimen/album_title_padding"
  40. android:paddingRight="@dimen/album_title_padding"
  41. android:textSize="@dimen/songs_count" />
  42. <ImageView
  43. android:id="@+id/overflow"
  44. android:layout_width="@dimen/ic_album_overflow_width"
  45. android:layout_height="@dimen/ic_album_overflow_height"
  46. android:layout_alignParentRight="true"
  47. android:layout_below="@id/thumbnail"
  48. android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
  49. android:scaleType="centerCrop"
  50. android:src="@drawable/ic_dots" />
  51. </RelativeLayout>
  52. </android.support.v7.widget.CardView>
  53. </LinearLayout>
UI design of the code given above
android
Step 8
Into the MainActivity.java, copy and paste the code given below. Java programming is the backend language for Android. Do not replace your package name, else an app will not run. The code given below contains my package name. MainActivity.java code is given below.
  1. package com.delaroystudios.cardview;
  2. import android.content.res.Resources;
  3. import android.graphics.Rect;
  4. import android.os.Bundle;
  5. import android.support.design.widget.AppBarLayout;
  6. import android.support.design.widget.CollapsingToolbarLayout;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.DefaultItemAnimator;
  9. import android.support.v7.widget.GridLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.support.v7.widget.Toolbar;
  12. import android.util.TypedValue;
  13. import android.view.View;
  14. import android.widget.ImageView;
  15. import com.bumptech.glide.Glide;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity extends AppCompatActivity {
  19. private RecyclerView recyclerView;
  20. private AlbumsAdapter adapter;
  21. private List<Album> albumList;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  27. setSupportActionBar(toolbar);
  28. initCollapsingToolbar();
  29. recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  30. albumList = new ArrayList<>();
  31. adapter = new AlbumsAdapter(this, albumList);
  32. RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
  33. recyclerView.setLayoutManager(mLayoutManager);
  34. recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
  35. recyclerView.setItemAnimator(new DefaultItemAnimator());
  36. recyclerView.setAdapter(adapter);
  37. prepareAlbums();
  38. try {
  39. Glide.with(this).load(R.drawable.cover).into((ImageView) findViewById(R.id.backdrop));
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. /**
  45. * Initializing collapsing toolbar
  46. * Will show and hide the toolbar title on scroll
  47. */
  48. private void initCollapsingToolbar() {
  49. final CollapsingToolbarLayout collapsingToolbar =
  50. (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
  51. collapsingToolbar.setTitle(" ");
  52. AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
  53. appBarLayout.setExpanded(true);
  54. // hiding & showing the title when toolbar expanded & collapsed
  55. appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  56. boolean isShow = false;
  57. int scrollRange = -1;
  58. @Override
  59. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  60. if (scrollRange == -1) {
  61. scrollRange = appBarLayout.getTotalScrollRange();
  62. }
  63. if (scrollRange + verticalOffset == 0) {
  64. collapsingToolbar.setTitle(getString(R.string.app_name));
  65. isShow = true;
  66. } else if (isShow) {
  67. collapsingToolbar.setTitle(" ");
  68. isShow = false;
  69. }
  70. }
  71. });
  72. }
  73. /**
  74. * Adding few albums for testing
  75. */
  76. private void prepareAlbums() {
  77. int[] covers = new int[]{
  78. R.drawable.album1,
  79. R.drawable.album2,
  80. R.drawable.album3,
  81. R.drawable.album4,
  82. R.drawable.album5,
  83. R.drawable.album6,
  84. R.drawable.album7,
  85. R.drawable.album8,
  86. R.drawable.album9,
  87. R.drawable.album10,
  88. R.drawable.album11};
  89. Album a = new Album("Maroon5", 13, covers[0]);
  90. albumList.add(a);
  91. a = new Album("Sugar Ray", 8, covers[1]);
  92. albumList.add(a);
  93. a = new Album("Bon Jovi", 11, covers[2]);
  94. albumList.add(a);
  95. a = new Album("The Corrs", 12, covers[3]);
  96. albumList.add(a);
  97. a = new Album("The Cranberries", 14, covers[4]);
  98. albumList.add(a);
  99. a = new Album("Westlife", 1, covers[5]);
  100. albumList.add(a);
  101. a = new Album("Black Eyed Peas", 11, covers[6]);
  102. albumList.add(a);
  103. a = new Album("VivaLaVida", 14, covers[7]);
  104. albumList.add(a);
  105. a = new Album("The Cardigans", 11, covers[8]);
  106. albumList.add(a);
  107. a = new Album("Pussycat Dolls", 17, covers[9]);
  108. albumList.add(a);
  109. adapter.notifyDataSetChanged();
  110. }
  111. /**
  112. * RecyclerView item decoration - give equal margin around grid item
  113. */
  114. public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
  115. private int spanCount;
  116. private int spacing;
  117. private boolean includeEdge;
  118. public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
  119. this.spanCount = spanCount;
  120. this.spacing = spacing;
  121. this.includeEdge = includeEdge;
  122. }
  123. @Override
  124. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  125. int position = parent.getChildAdapterPosition(view); // item position
  126. int column = position % spanCount; // item column
  127. if (includeEdge) {
  128. outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
  129. outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
  130. if (position < spanCount) { // top edge
  131. outRect.top = spacing;
  132. }
  133. outRect.bottom = spacing; // item bottom
  134. } else {
  135. outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
  136. outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
  137. if (position >= spanCount) {
  138. outRect.top = spacing; // item top
  139. }
  140. }
  141. }
  142. }
  143. private int dpToPx(int dp) {
  144. Resources r = getResources();
  145. return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
  146. }
  147. }
Step 9
Create AlbumsAdapter.java into the layout (app->java->your package name). Into the AlbumsAdapter.java copy and paste the code given below. Java programming is the backend language for Android. Do not replace your package name.
AlbumsAdapter.java code
  1. package com.delaroystudios.cardview;
  2. import android.content.Context;
  3. import android.support.v7.widget.PopupMenu;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.view.LayoutInflater;
  6. import android.view.MenuInflater;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.bumptech.glide.Glide;
  14. import java.util.List;
  15. public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {
  16. private Context mContext;
  17. private List<Album> albumList;
  18. public class MyViewHolder extends RecyclerView.ViewHolder {
  19. public TextView title, count;
  20. public ImageView thumbnail, overflow;
  21. public MyViewHolder(View view) {
  22. super(view);
  23. title = (TextView) view.findViewById(R.id.title);
  24. count = (TextView) view.findViewById(R.id.count);
  25. thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
  26. overflow = (ImageView) view.findViewById(R.id.overflow);
  27. }
  28. }
  29. public AlbumsAdapter(Context mContext, List<Album> albumList) {
  30. this.mContext = mContext;
  31. this.albumList = albumList;
  32. }
  33. @Override
  34. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  35. View itemView = LayoutInflater.from(parent.getContext())
  36. .inflate(R.layout.album_card, parent, false);
  37. return new MyViewHolder(itemView);
  38. }
  39. @Override
  40. public void onBindViewHolder(final MyViewHolder holder, int position) {
  41. Album album = albumList.get(position);
  42. holder.title.setText(album.getName());
  43. holder.count.setText(album.getNumOfSongs() + " songs");
  44. // loading album cover using Glide library
  45. Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);
  46. holder.overflow.setOnClickListener(new View.OnClickListener() {
  47. @Override
  48. public void onClick(View view) {
  49. showPopupMenu(holder.overflow);
  50. }
  51. });
  52. }
  53. /**
  54. * Showing popup menu when tapping on 3 dots
  55. */
  56. private void showPopupMenu(View view) {
  57. // inflate menu
  58. PopupMenu popup = new PopupMenu(mContext, view);
  59. MenuInflater inflater = popup.getMenuInflater();
  60. inflater.inflate(R.menu.menu_album, popup.getMenu());
  61. popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
  62. popup.show();
  63. }
  64. /**
  65. * Click listener for popup menu items
  66. */
  67. class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
  68. public MyMenuItemClickListener() {
  69. }
  70. @Override
  71. public boolean onMenuItemClick(MenuItem menuItem) {
  72. switch (menuItem.getItemId()) {
  73. case R.id.action_add_favourite:
  74. Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
  75. return true;
  76. case R.id.action_play_next:
  77. Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
  78. return true;
  79. default:
  80. }
  81. return false;
  82. }
  83. }
  84. @Override
  85. public int getItemCount() {
  86. return albumList.size();
  87. }
  88. }
Step 10
Create Albums.java into the layout (app->java->your package name). In Albums.java, copy and paste the code given below.
  1. package com.delaroystudios.cardview;
  2. public class Album {
  3. private String name;
  4. private int numOfSongs;
  5. private int thumbnail;
  6. public Album() {
  7. }
  8. public Album(String name, int numOfSongs, int thumbnail) {
  9. this.name = name;
  10. this.numOfSongs = numOfSongs;
  11. this.thumbnail = thumbnail;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public int getNumOfSongs() {
  20. return numOfSongs;
  21. }
  22. public void setNumOfSongs(int numOfSongs) {
  23. this.numOfSongs = numOfSongs;
  24. }
  25. public int getThumbnail() {
  26. return thumbnail;
  27. }
  28. public void setThumbnail(int thumbnail) {
  29. this.thumbnail = thumbnail;
  30. }
  31. }
Step 11
This is our user interface of the Application. Click Make Project.
android
Step 12
Run the Application, followed by choosing the virtual machine. Click OK.
Deliverables
Here, we have successfully created RecyclerView in an Android, using an Android Studio Application and executed it.
android
android
If you have any doubts, just comment below.