/**
   * Function used to ensure that the items of a given list row gets wrapped up and do not extend
   * the UI width and height
   */
  private void populateLinks(
      LinearLayout ll, ArrayList<RouteStep> collection, Route route, TextView realTimeText) {

    Display display = activity.getWindowManager().getDefaultDisplay();
    int maxWidth = display.getWidth() - 100;

    /*
    Only if route object contains steps array, allow wrap content
     */
    if (collection.size() > 0) {
      LinearLayout llAlso = new LinearLayout(activity.getApplicationContext());
      llAlso.setLayoutParams(
          new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
      llAlso.setOrientation(LinearLayout.HORIZONTAL);

      int widthSoFar = 0;
      for (int pos = 0; pos < collection.size(); pos++) {
        RouteStep samItem = collection.get(pos);

        TextView image = new TextView(activity.getApplicationContext());
        if (samItem.isTransit()) {
          image.setText(samItem.getDeparture().getTravelTime());

          /*
          Assign an ID to the textview so that when application
          gets the real time data, it can strike off the scheduled arrival time
           */
          image.setId(pos);

          /*
          If the route step is the BUS, check if its the first public transport
          step of the journey, then strike the scheduled bus arrival time
          with the actual arrival time of the BUS retrieved from our application
          server.
           */
          if (samItem.getTransportName() == R.string.tr_bus
              && (pos == 0 || (pos > 0 && !collection.get(pos - 1).isTransit()))
              && !TextUtils.isEmpty(realTimeText.getText())
              && realTimeText.getText().toString().compareTo("Not Available") != 0) {
            image.setPaintFlags(image.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
          }
        } else {
          /*
          Display the time when the user has to start this particular step
          of the journey.
           */
          if (pos == 0) {
            image.setText(route.getDeparture().getTravelTime());
          } else {
            RouteStep item = collection.get(pos - 1);
            image.setText(item.getArrival().getTravelTime());
          }

          // image = new ImageView(activity.getApplicationContext());
          // ((ImageView)image).setImageResource(samItem.getIconId());
          // image.measure(0, 0);
        }

        /*
        display an image corresponding to the mode of travel.
         */
        int id = samItem.getIconId();
        image.setCompoundDrawablesWithIntrinsicBounds(
            0, // left
            0, // top
            0, // right
            id); // bottom);
        image.setTextColor(Color.parseColor("#996633"));
        image.setTypeface(null, Typeface.BOLD);
        image.measure(0, 0);
        widthSoFar += image.getMeasuredWidth();

        /*
        if the components added so far has extended the UI width, wrap
        the contents of the corresponding list row
         */
        if (widthSoFar >= maxWidth) {
          ll.addView(llAlso);

          llAlso = new LinearLayout(activity.getApplicationContext());
          llAlso.setLayoutParams(
              new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          llAlso.setOrientation(LinearLayout.HORIZONTAL);

          llAlso.addView(image);
          widthSoFar = image.getMeasuredWidth();
        } else {
          llAlso.addView(image);
        }

        /*
        Display the transit vehicle line number and check if
        the components added so far has not extended the UI width. If yes then wrap
        the contents of the corresponding list row
         */
        if (samItem.getShortName() != "" && !samItem.getShortName().equals("")) {
          TextView name = new TextView(activity.getApplicationContext());
          name.setText(samItem.getShortName());
          LinearLayout.LayoutParams params =
              new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          params.setMargins(0, 50, 0, 0);
          name.setLayoutParams(params);
          name.setTextColor(Color.parseColor("#FFA500"));
          name.setTypeface(null, Typeface.BOLD);

          name.measure(0, 0);
          widthSoFar += name.getMeasuredWidth();

          if (widthSoFar >= maxWidth) {
            ll.addView(llAlso);

            llAlso = new LinearLayout(activity.getApplicationContext());
            llAlso.setLayoutParams(
                new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            llAlso.setOrientation(LinearLayout.HORIZONTAL);

            llAlso.addView(name);
            widthSoFar = name.getMeasuredWidth();
          } else {
            llAlso.addView(name);
          }
        }
        if (pos < collection.size() - 1) {
          ImageView next = new ImageView(activity.getApplicationContext());
          next.setImageResource(R.drawable.ic_action_next_item);
          next.measure(0, 0);
          widthSoFar += next.getMeasuredWidth();

          if (widthSoFar >= maxWidth) {
            ll.addView(llAlso);

            llAlso = new LinearLayout(activity.getApplicationContext());
            llAlso.setLayoutParams(
                new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            llAlso.setOrientation(LinearLayout.HORIZONTAL);

            llAlso.addView(next);
            widthSoFar = next.getMeasuredWidth();
          } else {
            llAlso.addView(next);
          }
        }
      }

      ll.addView(llAlso);
    }
  }