Android Basics – Learn how to create Recycler View

So far you have created an Adapter and added a View Holder and also code to onBindViewHolder.

Now it’s time to create a Recycler View which will use this Adapter. It will also pass data to this Adapter.

In order to do this you can create a new Activity, for the purpose of understanding. Let’s call this MaterialActivity. You will call this from the MainActivity on click of a button.

In the layout file for the new Activity you need to add the RecyclerView.


In the Activity class you need to set the Adapter.

public class MaterialActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        String[] inputData = {"Hello Sam", "Hello John", "Hello Riki"};

        RecyclerView recyclerView = (RecyclerView) findViewById(;

        SampleAdapter adapter = new SampleAdapter(inputData);


Source code is available here.

In the next Article we will take this further.

Android Basics – Learn how to use the onBindViewHolder method to bind the data to the View

So far you have created an Adapter for the RecyclerView. You have also added a View Holder.

In order to display data you need to write code to the onBindViewHolder method. This method will map the data to the Card View.

public void onBindViewHolder(ViewHolder holder, int position) {
    CardView cardView = holder.mCardView;
    TextView cardText = (TextView) cardView.findViewById(;

Source code is available here.

Android Basics – Adding a ViewHolder to the RecyclerViewAdapter

When you create a RecyclerView Adapter then you need to create a view holder by extending the RecyclerView.ViewHolder class.

Like the name suggests a ViewHolder is used to hold and display the View. Recycler view maintains a set of these views.

The number of these holders depends on the size of the view.

When the Recycler view is first constructed it builds this set by calling the onCreateViewHolder() method.

public static class ViewHolder extends RecyclerView.ViewHolder{
    CardView mCardView;
    public ViewHolder(View itemView) {
        mCardView = (CardView) itemView;

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    CardView cardView = (CardView) LayoutInflater.from(parent.getContext())

    return new ViewHolder(cardView);

Source Code is available here

Android Basics – Create the basic Adapter for RecyclerView

To create a basic Adapter which can be used by the RecyclerView create a new class and extend RecyclerView.Adapter

Once you do this you need to override the following three methods

  • The getItemCount method is used to return the number of items in the data set.
  • The onCreateViewHolder method is used to create the views.
  • The onBindViewHolder method is used to bind views to data.
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return null;

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


public int getItemCount() {
    return 0;

Source Code is available here

Android Basics – An introduction to the Recycler View Adapter

Recycler View is an efficient and an advanced version of a List View. It can be used to display a list of Card Views.

This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.

Courtesy –

In order to do this it uses an Adapter and a Layout Manager. The Adapter needs to be created by you and should be a sub class of RecyclerView.Adapter

The Adapter is responsible to create and configure the View.

Android Basics – Learn how to create a Card View

The last Article introduced you to Material Design basics. In this Article you will learn how to create a Card View.

Steps to add a CardView

  • Add support libraries – and recyclerview
  • Create a layout xml file for the card view.
  • Refer to the below code snippet
<?xml version="1.0" encoding="utf-8"?>


    <LinearLayout android:layout_width="match_parent"

            android:text="Hello Card View"



Source Code is available here

Android Basics – An Introduction to Material Design

Material Design was launched with Android 5.0 (API level 21). The purpose being to provide consistent look and feel to all the Apps. So your App looks consistent with a Google App like Gmail.

It uses Animation and 3D effects.

Android provides the following elements for you to build material design apps:

  1. A new theme – Provides a new style for your App.
  2. New widgets for complex views
  3. New APIs for custom shadows and animations

Two of the most important Material Design widgets are Recycler and Card Views.

RecyclerView widget is a more pluggable version of ListView that supports different layout types and provides performance improvements.

CardView widget lets you display important pieces of information inside cards that have a consistent look and feel.

In addition to the X and Y properties, views in Android now have a Z property. Views with higher Z values cast bigger shadows and appear on top of other views.
To learn more about the principles of Material Design visit the following URL

Source : Android Documentation

Calling the Bound Service from Main Activity

In the last Article you learnt how to bind MainActivity with BoundService.

Time for you to call the displayDistance method to display the distance traveled. You can create a new method which will return the distance computed. This method will create a Handler and invoke the Service every second.

private void displayDistance() {
    final Handler handler = new Handler(); Runnable() {
        public void run() {
            double distance = 0;
            if(mDistanceTraveledService != null){
                distance = mDistanceTraveledService.getDistanceTraveled();
            handler.postDelayed(this, 1000);

Next add a TextView in the Layout XML file to display this.


Full source code is available here


Android Basics : Learn how to bind a Bound Service to an Activity using ServiceConnection

In the previous Article you saw how to add the LocationListener to the Service class. You created a method getDistanceTraveled to return the distance.

Next step is to call this method from the MainActivity. So how to do this ?

You need to create a ServiceConnection object and implement its methods.

ServiceConnection mServiceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        DistanceTraveledService.DistanceTravelBinder distanceTravelBinder = 
        mDistanceTraveledService = distanceTravelBinder.getBinder();
        bound = true;

    public void onServiceDisconnected(ComponentName name) {
        bound = false;

Once you have the ServiceConnection object you need to create an intent and call bindService method.

protected void onStart(){
    Intent intent = new Intent(this, DistanceTraveledService.class);
    bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

Source code is available here

Android Basics : Learn how to track Location changes by adding a LocationListener

In order to use the Location Service you need to do the following:

  • Implement LocationListener
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        if (lastLocation == null) {
            lastLocation = location;
        distanceInMetres += location.distanceTo(lastLocation);

    public void onStatusChanged(String provider, int status, Bundle extras) {


    public void onProviderEnabled(String provider) {


    public void onProviderDisabled(String provider) {

  • Create LocationManager using the Listener to start tracking user location changes
LocationManager locationManager = (LocationManager)
if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != 
        && ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_COARSE_LOCATION) != 
            PackageManager.PERMISSION_GRANTED) {
  • Add Permissions to the Manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

The Source Code for this implementation is available here