Thursday 24 November 2016

Android RecyclerView vs ListView | Differences | Comparison

Android is an evolving platform. With the official RecyclerView support v7 library. As per the official documentation RecyclerView is a major enhancement over ListView. It contains many new features like ViewHolder, ItemDecorator, LayoutManager, and SmoothScroller. But one thing that certainly gives it an edge over the ListView is; the ability to have animations while adding or removing an item. Here in Android RecyclerView vs ListView sum up I would compare the various features of RecyclerView and ListView and form an opinion why it should be used in new implementations.

Differences Between RecyclerView and ListView

1. View Holders

In ListView, defining view holders was a suggested approach for keeping references for views. But it was not a compulsion. Although by not doing so, ListView used show stale data. Another major drawback of not using view holders could lead to a heavy operation of finding views by ids every time. Which resulted in laggy ListViews.
This problem is solved in RecylerView by the use of RecyclerView.ViewHolder class. This is one of the major differences in RecyclerView and ListView. When implementing a RecyclerView this class is used to define a ViewHolder object which is used by the adapter to bind ViewHolder with a position. Another point to be noted here, is that while implementing the adapter for RecyclerView, providing a ViewHolder is compulsory. This makes the implementation a little complex, but solves the issues faced in ListView.

2. Layout Manager

When speaking of ListViews, only one type of ListView is available i.e. the vertical ListView. You cannot implement a ListView with horizontal scroll. I know there are ways to implement a horizontal scroll, but believe me it was not designed to work that way.
But now when we look at Android RecyclerView vs ListView, we have support for horizontal collections as well. In-fact it supports multiple types of lists. To support multiple types of lists it uses RecyclerView.LayoutManager class. This is something new that ListView does not have. RecyclerView supports three types of predefined Layout Managers:
  • LinearLayoutManager – This is the most commonly used layout manager in case of RecyclerView. Through this, we can create both horizontal and vertical scroll lists.
  • StaggeredGridLayoutManager – Through this layout manager, we can create staggered lists. Just like the Pinterest screen.
  • GridLayoutManager– This layout manager can be used to display grids, like any picture gallery.

3. Item Animator

Animations in a list is a whole new dimension, which has endless possibilities. In a ListView, as such there are no special provisions through which one can animate, addition or deletion of items. Instead later on as android evolvedViewPropertyAnimator was suggested by Google’s Chet Haase in this video tutorial for animations in ListView.
On the other hand comparing Android RecyclerView vs ListView, it has RecyclerView.ItemAnimator class for handling animations. Through this class custom animations can be defined for item addition, deletion and move events. Also it provides aDefaultItemAnimator, in case you don’t need any customizations.

4. Adapter

ListView adapters were simple to implement. They had a main method getView where all the magic used to happen. Where the views were bound to a position. Also they used to have an interesting method registerDataSetObserver where one can set an observer right in the adapter. This feature is also present in RecyclerView, but RecyclerView.AdapterDataObserver class is used for it. But the point in favor of ListView is that it supports three default implementations of adapters:
Whereas RecyclerView adapter, has all the functionality that ListView adapters had except the built in support for DB cursors and ArrayLists. In RecyclerView.Adapter as of now we have to make a custom implementation to supply data to the adapter. Just like a BaseAdapter does for ListViews. Although if you wish to know more about RecyclerView adapter implementation, please refer to Android RecyclerView Example.

5. Item Decoration

To display custom dividers in a ListView, one could have easily added these parameters in the ListView XML:
The interesting part about Android RecyclerView is that, as of now it does not show a divider between items by default. Although the guys at Google must have left this out for customization, intentionally. But this greatly increases the effort for a developer. If you wish to add a divider between items, you may need to do a custom implementation by using RecyclerView.ItemDecorationclass.
Or you can apply a hack by using this file from official samples: DividerItemDecoration.java

6. OnItemTouchListener

Listviews used to have a simple implementation for detection of clicks, i.e. by the use of AdapterView.OnItemClickListenerinterface.
But on the other hand RecyclerView.OnItemTouchListener interface is used to detect touch events in Android RecyclerView. It complicates the implementation a little, but it gives a greater control to the developer for intercepting touch events. The official documentation states, it can be useful for gestural manipulations as it intercepts a touch event before it is delivered to RecyclerView.

7. Conclusion

I would like to conclude this Android RecyclerView vs ListView comparison by saying RecyclerView is filled will a lot customizable functionality. It can be very useful for implementing complex lists or grids. But also its a little difficult to implement as compared to ListView as it contains multiple classes where implementation is required. Hope this helped you decide. Please connect with us for more updates.


Thank You :)

Monday 21 November 2016

Android Development Tutorial From Starting Point

Hello friends , Don’t worry if you don’t even know the ‘A’ of Android App Development. I will try to tell you everything about Android Application Development. So this is the very first post of this series so in this post I will tell you about configuring your system for android application development tutorial.


What you need for this Android Application Development Tutorial Series

  • A Computer (Whether it is MAC , Windows , Ubuntu , etc)

What you should already know before starting?

  • JAVA Programming Language. You should know the basics concepts of JAVA.

Software Requirements

  • Java Development Kit (JDK)
  • Android Studio with SDK

Installing Java Development Kit

  • First we need to install JDK. So get the JDK from this link.
  • In my case I have downloaded jdk8 64bit for windows, because I am using Windows Operating System
  • Install JDK (next -> next -> next -> finish as we always do :P)
  • Now we need to set an environment variable

Setting JAVA_HOME variable

  • Right click on My Computer (This PC) and click on properties
my computer properties
  • A window will open, from left click on Advanced System Settings 
advance system settings
  • Now from the new window that will open click on environment variables
system properties
system properties
  • Now from the new window in System Variables click on new
system variables
  • Now in Variable Name write ‘JAVA_HOME’ and in value copy the path of your JDK Installation directory usually inside C:\Program Files\Java\jdkxxx
new system variable
  • Now apply the changes and its done.

Installing Android Studio

  • Download Android Studio from here
  • You will again get an executable file to install android studio
  • Install android studio (next -> next -> finish)
  • and thats it so simple
  • Now you can open Android Studio from start menu
  • The final part of this post is to create an emulator to run and debug android apps

Creating an Android Emulator for Debugging and Testing

  • Though the installation of Android Studio itself creates an Android Emulator but for this series of tutorial I will be using GenyMotion
  • GenyMotion is fast and smooth and consumes less memory so I will be using GenyMotion.
  • You can see the details of Installing GenyMotion from here
  • After successfully Installing GenyMotion we can start our Android Application Development Tutorial.

Creating Your First Android Application

  • Open Android Studio
  • Click on Start a New Android Studio Project
welcome to android studio
  • Now you have to fill some details
  • The first thing is name of your application
  • Next is company domain, you can enter any domain it will be use to create the java packages in your project
  • And at last you have to choose a location in your hard drive to store your project
new project
  • After filling out the above details click on next
  • Now you have to select the minimum version of android for your application
  • It is by default 4.0.3 (IceCreamSandwich) that means the application you are creating will not be supported by devices having the earlier versions of android from android 4.0.3
  • Now Click on Next
target android devices
  • Now you have to select an Activity for your Application. In android an activity is the screen you see in the application. You can have multiple activities in your application. For now we are creating an activity.
  • Here you can see a number of predefined activity templates, don’t bother much we are selecting the first one which is Blank Activity.
  • Now click next
  • add an activity
    • Now you will get the option to customize the Activity
    cutomize the activity
    • All the fields are filled already with default so you do not need to change anything unless you don’t want
    • The first thing is Activity Name: It is the name of the activity you are creating. A java file will be created with this name. You can change the name if you want but for now we are not changing the name.
    • Next is Layout Name: In android we separate the Layout Designing and Coding with XML and JAVA. So for your Activity’s visual design a separate layout file will be created. And what you are seeing here is activity_main this is the name of the xml file for your layout. You can also change this but the rule is you can only use small letters and underscores (_) any other characters are not allowed.
    • Next is Title: This is the Title which will appear at the top of your application. Usually it is our Application’s Name.
    • The Last is Menu Resource Name: Android studio will also create a menu resource xml file for the menu of your application. And this is the name of the menu file for your android application.
    • Don’t worry if you are having difficulties understanding these things we will  cover this in detail in my upcoming articles
    • And you can also change these settings after creating the project so don’t bother much and just click on Finish.
    • Now wait for few minutes and you will see the following screen
    android studio project
    • As you can see it is already Hello World!. So now we have an app with a single activity.
    • Don’t bother much now we will try to cover each and everything, but for this post I am wrapping up this tutorial.
    • Now just see how we can run our application. As I have already said we will use GenyMotion. So open GenyMotion from start menu and start your virtual device.
    • Now in Android Studio click on the play button from the top
    • Now wait for a while until don’t see the following window
    choose device
    • Now as you can see I have my Genymotion Virtual Device is running. Select the device and click on ok.
    • See your Virtual Device Now
    genymotion
    Bingo! You have just successfully executed your very first Android Application. So thats it for this post of this Android Application Development Tutorial Series. In upcoming tutorials we will start understanding android application development. So stay tuned and share the post if you liked it. Thank You 🙂

Monday 14 November 2016

Firebase Cloud Messaging for Android

Creating an Android Studio Project

  • As we do always 😛 create a new android studio project. Once the project is loaded copy the package name you can get it from the AndroidManifest.xml file.

Getting a Configuration File

firebase cloud messaging tutorial
  • Now put your app name and select your country.
firebase cloud messaging tutorial
  • Now click on Add Firebase to Your Android App.
add firebase to android
  • Now you have to enter your projects package name and click on ADD APP.
  • After clicking add app you will get google-services.json file.

Adding Firebase Messaging to Your Project

  • Now come back to your android project. Go to app folder and paste google-services.json file.
firebase cloud messaging tutorial
  • Now go to your root level build.gradle file and add the following code.
  • Inside app level build.gradle file make the following changes.
  • Now sync your project.

Implementing Firebase Cloud Messaging

  • Create a class named  MyFirebaseInstanceIDService.java and write the following code.
  • Now create MyFirebaseMessagingService.java and write the following code.
  • Now we have to define the above services in our AndroidManifest.xml file. So go to manifest and modify as follows.
  • Thats all. Now run your app (Make sure your device is having google play service or else it won’t work).
  • After running you will see the token in the logcat. Copy the token.
firebase cloud messaging tutorial

Sending Push Notification using Firebase Console

  • Go to firebase console and select the app you created.
  • From the left menu select notification.
  • Click on new message.
  • Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device
firebase cloud messaging tutorial
Firebase Cloud Messaging Tutorial
  • Bingo! It is working absolutely fine. If you are having trouble then you can get my source code from the link given below.
  • I hope you all like it if any Queries please comment it below.