If you are using the latest Android Studio versions, there will be a CheckBox with the text “Use androidx.* artifacts”. Make sure that’s checked, which creates our project with androidx library.
are these thoughts running through your head? then Go on.
you know something about RecyclerView then Refresh your knowledge by reading this post.
In this article, you will learn about RecyclerView, RecyclerView Adapter, and a simple example that helps you to create your one.
At last, I will teach you how to use Checkbox with RecyclerView. Yes, you can scroll fearlessly without thinking about losing the selected states in CheckBox.
From the name, we can understand that it’s an Android View. Actually, It is a ViewGroup with scroll ability which works based on adapter principles.
In simple words, It helps us to show a large no. of data collections just like ListView and GridView.
If RecyclerView wants to show large no.of items, it does not create the same amount of views. It just creates the viewholders which hold the inflated view needed to fill the screen and some more.
After that when the user scrolls, it makes use of cached views. Yes, It recycles views, again and again, that’s why it got the name “RecyclerView”.
As a successor, It is far away from ancestors(ListView, GridView) by performances and abilities. By more features, it becomes a little bit complex too.
Let’s look up the history, ListView has been part of Android since 1.0, but each os releases, new features got added. Not in the case of ListView.
ListView simply defeated by developer’s needs such as different types of layouts and animation support. That’s where RecyclerView was born.
Finally in 2014 Android Lolipop introduced RecyclerView as a standalone support library.
After the arrival of the AndroidX library (after API level 28.0.0), RecyclerView changed its package name android.support.v7.widget.RecyclerView to androidx.recyclerview.widget.RecyclerView.
If you are using Android Studio, just add it to gradle and use.
8 Differences Between ListView And RecyclerView In Android
1. ViewHolder pattern
ViewHolder pattern is a strategy used to store view references in memory.
It reduces the number of calls to complicated and tedious findViewById(), which makes scrolling smooth.
In the case of ListView, it was optional, you can build your favorite list without any use of ViewHolder.
But when it comes to RecyclerView side, google developers make it as default for RecyclerView.
2. Re-Using views
Both ListView and RecyclerView support Reusing views. On the ListView side, it gives the option to reuse.
Using the above example code, we can check the convertView is null or not. If it is null, that means there is no recycled view. Then we can inflate the layout.
If it is not null, means that not the first time, so we can use convertView and avoid layout inflation.
3. Layout Types
With ListView, you can easily make a list with simple and less code.
Code is a bit complex with RecyclerView, but it provides more options: horizontal, vertical list, grid, and staggered grid
You can also create your custom layout manager using RecyclerView.LayoutManager.
In the animation section, RecyclerView is far ahead from ListView. It’s not easy for beginners to make animations in ListView.
But RecyclerView shows Fade in, Fade out, translate and crossfade animations when you add, delete, move and update an item in RecyclerView.
This is default in RecyclerView because it internally uses the DefaultItemAnimator class. That’s why they gave that name.
If you don’t like these animations and want to make your animations make use of RecyclerView.ItemAnimator class.
ListView has divider by default.
The android:divider and android:dividerHeight attributes or setDivider(),setDividerHeight() helps you to make custom divider in ListView.
But RecyclerView doesn’t give these options.
But you can use the DividerItemDecoration class to make a simple divider or Make use of RecyclerView.OnItemDecoration class to decorate the view.
6. Click Events
One of the main disadvantages we face with RecyclerView is that it lacks OnItemClickListener. Because it’s not a subclass of AdapterView like ListView.
It provides RecyclerView.OnItemTouchListener to capture click events. It gives more control to the developer.
You can also use OnClickListener with views on the item.
When notifying adapter, while data change occurs we can call notifyDatasetChanged() with ListView.
But ListView doesn’t know what had happened? It can be the addition, deletion or change of items in the dataset but don’t know what was happened.
For RecyclerView there are lots of notify* methods to inform, including notifyDatasetChanged().
Using the appropriate notify* method can invoke better animation in RecyclerView.
8. Header and Footer
It is easy to add Header and Footer in ListView using addHeaderView() and addFooterView().
But RecyclerView doesn’t have these methods instead it supports different types of view.
Finally, RecyclerView supports Nested Scrolling.
I think that’s enough for the comparison. So use them based on your needs. If you are a beginner in Android App Development, Learn ListView first then go with RecyclerView.
How To Use RecyclerView In Android?
RecyclerView is one of the most valuable widgets in Android development. Out of the box, RecyclerView is lazy. Because it has given responsibilities to different classes.
So before Using RecyclerView, you should know about these classes.
This is the most important class in RecyclerView. Same as other AdapterView, It connects the View(RecyclerView) and data.
RecyclerView has no default adapters, So you need to create a custom class by extending Recyclerview.Adapter.
It is responsible for
Binds data to ViewHolders.
Informs Recyclerview about dataset changes.
Most of the important methods in this class are:
i)OnCreateViewHolder() : This method is called when RecyclerView needed to create a viewholder.
ii)OnBindViewHolder(): Calls whenever needed to bind data with view in viewholder.
iii)getItemCount() : Used to return total number of items in the dataset (ArrayList, array etc).
iv)getItemViewType(): it returns the viewtype based on the position.
As the name says, it helps RecyclerView to manage layout by positioning each item.
Unlike ListView, RecyclerView shows child items in Horizontal, Vertical list, grid, and staggered grid because of LayoutManager.
Scrolling responsibility is also own by LayoutManager. When you scroll up in RecyclerView, LayoutManager moves your items up.
It provides different classes for arranging items:
i)LinearLayoutManager: Creates vertical and horizontal lists.
ii)GridLayoutManager: Creates grid layouts.
iii)StaggeredGridLayoutManager: Creates StaggeredGrid layouts.
iv)WearableLinearLayoutManager: This class is for wearable devices.
The name implies, it decorates RecyclerView’s item or view. It provides not only a divider, Using this abstract class you can draw four sides of the item. When using the divider as a view in XML, it decreases performance. That’s where the importance of this class comes through.
Version 25.1.0 Support library introduces the DividerItemDecoration class, to make a simple divider. Use addItemDecoration() method to add with RecyclerView. This class supports both horizontal and vertical orientations, so you can use it with any of your lists without any worries.
Google developers gave more importance to animations in RecyclerView, So RecyclerView provides RecyclerView.ItemAnimator class for handling animations.
You can make custom animations using this class.
Just I said in ListView vs RecyclerView comparison, RecyclerView animates items when item adds, delete or any other move event happens because it internally uses DefaultItemAnimator.
But you can not see these animations when RecyclerView loading for the first time.
You can consider ViewHolder as something which holds View.
Unlike ListView, we need to make custom ViewHolder class by extending RecyclerView.ViewHolder class.
In this subclass, we must store the item view’s references. This ViewHolder class contains additional information like its position in the layout.
Simple RecyclerView Example With CardView In Android Studio – OnClickListener Included
In this example, I will explain how to create RecyclerView in Android Studio in 10 simple steps with a detailed explanation. The final output will look like below.
I assume that you have created a Project in Android Studio, then add these
STEP 1 – Add Dependencies In build.gradle
RecyclerView and CardView are support libraries, so you must add dependencies in your build.gradle file, If your app needs to support above API 28.0.0, you must use AndroidX libraries. otherwise, you can continue with the support library.
Just like ListView, just add it to your layout. here activity_main.xml
This code creates a RecyclerView widget with id “recyclerview”.
android:scrollbars=”vertical” – It creates vertical scrollbar to the RecyclerView. Otherwise user never get to know how long the RecyclerView is.
Use android.support.v7.widget.RecyclerView as root widget if you are using the support library, attributes are same as above.
STEP 3 – Create Row Layout For RecyclerView
In this step, we will create a layout and RecyclerView will show it as child items. Right-click on the layout folder in res directory. New->Layout Resource File-> put file name as row_layout.xml and put below code.
This code creates TextView inside CardView. You can use app: namespace to specify support library attributes.
if you are using support library, Use android.support.v7.widget.CardView.
STEP 4 – Create An Array And Pass It To RecyclerView Adapter
RecyclerView adapter needs data and layout. So here, we will create an array of months and pass it to the adapter class, which you are going to make in the next step.
STEP 6 – Create A Custom ViewHolder Class And Extend Adapter Class With RecyclerView.Adapter
In this step, You will create an inner ViewHolder class by extending RecyclerView.ViewHolder. After initializing views in ViewHolder, makes RecyclerViewAdapter extends RecyclerView.Adapter with ViewHolder as the generic parameter. Just like below
Genymotion is a third party Android Emulator which helps developers to run and test apps. When compared to Android default emulators, It boots fast and quickly runs your app. It uses x86 architecture and make use of OpenGL hardware acceleration, that gives us a smooth operation.
Let’s learn, how to create an account on genymotion.com and download Genymotion.
How to create an account and download Genymotion?
Search “genymotion free” on Google or go to “https://www.genymotion.com/fun-zone/”
Click on the button named “Download Genymotion Personal Edition”.
If you already have a Genymotion account, then you can sign in with your username and password. Otherwise, click on Create an account.
Fill up the form with valid details.
Go to your registered email account and click on the link like above.
Click on the link “Download Genymotion for personal use“.
I recommend downloading genymotion with Virtual box. If the virtual box is already installed on your pc, then you can select the second option. If you are a Mac or Ubuntu user, scroll down to see the download links.
Then click on the download button based on the operating system.
Download Genymotion virtual devices
After downloading, Install Genymotion just like any other software in Windows. At last, launch Genymotion.
If you skip these step or you already installed Genymotion, then double click on the Genymotion icon and launch.
When Genymotion launches, you can see a lot of virtual device templates. Select any one of them and click on the 3 vertical dot icon.
Click “install” from the menu.
You can configure the Genymotion virtual device here.
if you want the virtual keyboard tick on the checkbox Use virtual keyboard for text input, or you can use PC’s Keyboard. Click Install
After the download completion, click on the three vertical dot menu on the right side and click on start.
Genymotion virtual device started to load.
Genymotion has successfully installed on your PC.
Next section will discuss on Genymotion integration with Android Studio.
Download Genymotion plugin for Android Studio
Open Android Studio, go File->Settings
1. Click on Plugins
2. Search Genymotion in search dialog box.
3. If genymotion not found, Click on Search repositories or Browse repositories.
3 and 4 are leading to same page.
Click the install button.
Restart Android Studio for changes to take effect.
Click on the restart button
After the restart, click on the genymotion icon.
Choose Virtual devices from the dialog box and click on Start
Now click on the run button, genymotion virtual device will be shown on chooser dialog. Select and Click OK.
Done. Genymotion is successfully integrated with Android Studio. Now you can test all your application using the same steps.
How to install gapps or google play services on Genymotion
So many apps rely on google play services. Developers of Genymotion know that, So the latest version of Genymotion arrives with a widget named Open GAPPS. This widget simply installs play store in one click. Let me show you how to install play store on Genymotion.
Genymotion provided with a GAPPS icon to install play store. Click on the icon.
Click on Restart now, it will redirect to GApps website.
That’s all, play store is successfully installed.
Install your favorite apps and games now.
Why Android Studio not detecting Genymotion device
After the successful installation of Genymotion, I too have faced this problem. Do the below steps to solve
Open genymotion software, on the top-left side, click on genymotion.
Click on settings
Click on ADB
Select the radio button with the label “Use custom Android SDK Tools” , locate your Android SDK through BROWSE button, When the location reaches Android-SDK folder, select and open. If you have selected the proper SDK folder, then it turns out to say this folder is valid.
That’s all. Now Android Studio can launch Genymotion easily.
By reading this post, you already understood that Genymotion is far better than Android default emulator. It boots fast and runs your app in less time, it also provides lots of other features. Although, It lacks wearable support. For making a wear app, you must use Android default ones.
So all I want to say is just give it a try if you never have used Genymoion. Let us know your experience with Genymotion and if you enjoyed reading this article, consider sharing it on Facebook, Twitter, and Pinterest.
</textview></linearlayout></code><button android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Launch New Activity"android:textstyle="bold"><code>
1. Using Strike – Android TextView StrikeThrough XML example
Like Html, <Strike> tag easily creates a horizontal line through our TextView content. Create a string element in strings.xml and place text which you want to get strikethrough between <strike> opening tag and closing tag.
3.2 Using SpannableString – TextView StrikeThrough Example
We can control strikethrough where to appear in a text. For that, provide start and endpoints of the text. Here I StrikeThrough the content “StrikeThrough” using 4 as the start point and 17 as the endpoint.
val content2="3.2 StrikeThrough Using SpannableString"
Stringhtml="<u>Underline using Html.fromHtml()</u>";
But Html.fromHtml(resource : String) was deprecated in API 24. So now you can use HtmlCompat.fromHtml() method. It’s available in both android.support.v4.text.HtmlCompat, and androidx.core.text.HtmlCompat.