What is Android and what is an Android phone?

Android is now over five years old and despite the little green robot android peeking out of phone shops up and down the highstreet, there are still those who don’t know what it is or what it’s all about.

Coding Horror

I’ve come to discover that people who struggle to code don’t just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

Desigen Pattens

Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development.

Wanna Make Game...!

Highly praised tutorial will explain everything in detail to cater to both beginners and advanced programmers alike. When you walk away from this course, you will have created the above game in Java and ported it to Android.

Web Services

Web services are open standard ( XML, SOAP, HTTP etc.) based Web applications that interact with other web applications for the purpose of exchanging data

Monday, August 13, 2012

Android - Technically what is it?


  1. It is an operating system for mobile
  2. Interestingly, it is not yet another OS, but component based
    OS
  3. It has an integrated Java Virtual Machine
  4. System interfaces are exposed only through Java libraries
  5. It is based on the Linux Kernel
  6. An Android SDK is provided to let developers build applications on top of the OS but not extend the OS itself
  7. The SDK includes

    • Android packages
    • sample files
    • Documentation
    • A Simulator
The offical site of Android broadly classfies the technical parts of the OS as
consisting of
  1. Linux Kernel
  2. Android Runtime
  3. Libraries
  4. Application Framework
over which the core applications run as well as the applications developed by anyone using this framework 

Thursday, May 31, 2012

Android LIstView and Custom Adapter


Introduction

This tutorial will show you how to use ListView to display selectable lists of non trivial data, using complex cell renderers. The ListView is a selectable list. You can attach a variety of data models to it and load different display layouts (cell renderers). You can create your own model and cell renderer. This model-view combo is called an Adapter. In this tutorial, I will show you how to extend create your own Adapter from scratch, and create your own cell renderers from scratch as well.

What is a ListView?

What’s the difference between a TableLayout and a ListView? Items in a TableLayout are not ‘selectable’ (unless they are buttons or text areas that support keyboard focus), however, each row of a ListView can be selected. It’s just like a Swing JList. You can attach item selection listeners to a ListView to know when the user focuses in on a particular row of a list. The biggest difference between a Swing JList and a ListView is that the model view controller separation in JList is not there in ListView. A ListView’s adapter, holds all the list’s underlying data as well as the Views necessary to render each row of the ListView. However, there are many similarities with JList, for example, when your underlying data model changes, you have to fire an event to notify the adapter’s listeners that the underlying data has changed, and the views should be refreshed. The ListView does not need to be added to a ScrollView since it automatically supports scrolling.


You have to be careful between touch mode and D-pad mode input into a ListView, read this for more information. This affects the way your ListView will respond to user input by touch or by D-Pad movement, the gist of it is use theonClickListener to respond to user input, which will work for D-Pad as well as touch input events.
You also have to be careful about setting background colors and 9patch images on a ListView, read this for more information.



Example with source code

In the following example, I will show you how to create a ListView that uses a custom adapter to display weather data. The underlying weather data is stored in a Hashtable of Hashtables. At the top level, the key is a String that represents the zipcode. The Hashtable value for this key (zipcode) contains the current temperature, humidity, and an icon that represents the weather conditions. These are all strings as well. The ListView and adapter work together to display a list of these weather conditions for various zip codes. To render this weather data, the list cell renderer uses a TableLayout to display these weather conditions – each cell is composed of a 2 row table: the first row has 2 cells – icon and temperature, the second row has 1 cell – the humidity.



The following Activity creates a ListView (with this weather data adapter) and displays it:
@Override protected void onCreate(Bundle bundle) {

  super.onCreate(bundle);

  // window features - must be set prior to calling setContentView...
  requestWindowFeature(Window.FEATURE_NO_TITLE);

  setContentView(PanelBuilder.createList(this));

}
PanelBuilder is a static class with helper methods to assemble the ListView and it’s underlying adapter:
public static View createList(Activity activity) {

  // create the ui components
  LinearLayout mainpanel = new LinearLayout(activity);
  ListView listview = new ListView(activity);

  // setup the mainpanel
  {
    // apply view group params for the activity's root pane
    LayoutUtils.Layout.WidthFill_HeightFill.applyViewGroupParams(mainpanel);
    // set animation layout on the mainpanel
    AnimUtils.setLayoutAnim_slideupfrombottom(mainpanel, activity);
  }

  // setup the listview and add to the mainpanel
  {
    LayoutUtils.Layout.WidthFill_HeightFill.applyLinearLayoutParams(listview);

    bindListViewToAdapter(activity, listview);

    AnimUtils.setLayoutAnim_slidedownfromtop(listview, activity);

    mainpanel.addView(listview);
  }

  // return the mainpanel
  return mainpanel;

}
Some notes on the code:
  1. LayoutUtlis is used to quickly attach LayoutParams to the LinearLayout and ListView widgets.
  2. AnimUtils is used to apply layout animation sequences to the ViewGroups – mainpanel and listview.
  3. The ListView is embedded into a LinearLayout and then returned to the caller, to be placed on the Activity.
AnimUtils is a utility class that I’ve written (you can get it in AndroidUtils) to make it easier to assign layout animations on Viewgroup objects/containers (LinearLayout, TableLayout, ListView, etc). It can be used to assign different animation sequences to these ViewGroups very easily. With Android’s animation framework, it’s possible to enable layout animation, without you having to write the code. More on the animation framework in this tutorial.
Need more help? developerlife.com offers training courses to empower you, and consulting services to enable you.
Let’s look at the implementation of binListViewToAdapter() next:
/** create the list data, and bind a custom adapter to the listview */
private static void bindListViewToAdapter(Activity ctx, ListView listview) {

  final WeatherDataListAdapter listModelView = new WeatherDataListAdapter(ctx, listview);

  // bind a selection listener to the view
  listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
      listModelView.setSelected(position);
    }
    public void onNothingSelected(AdapterView parentView) {
      listModelView.setSelected(-1);
    }
  });

}
Some notes on the code:
  1. The ListView adapter (model + cell renderer) is called WeatherDataListAdapter, and it’s created here with some default data and then attached to the ListView already created above.
  2. A selection listener is attached to the ListView so that when the user traverses through the list, the adapter (model + partial view) can respond by updating the UI. This selection event triggers a model change event that causes the UI to be updated with the new selection. My cell renderer implementation renders a selected sell differently than an unselected cell, which is why this is even necessary. If your renderer doesn’t care about selection state to render the current row, then this step is not necessary.
Finally, let’s look at the WeatherDataListAdapter class, which extends BaseAdapter. The BaseAdapter class is a good skeleton class to extend in order to provide an implementation of your ListView adapter. The BaseAdapter doesn’t make any assumptions about what your data looks like, but it provides abstract methods for you to override to allow the ListView to traverse your data model.
public class WeatherDataListAdapter extends BaseAdapter {

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// data members
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/** holds all the weather data */
private Hashtable<String, Hashtable<String, String>> _data = new Hashtable<String, Hashtable<String, String>>();

/** holds the currently selected position */
private int _selectedIndex;
private Activity _context;

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// constructor
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/**
 * create the model-view object that will control the listview
 *
 * @param context  activity that creates this thing
 * @param listview bind to this listview
 */
public WeatherDataListAdapter(final Activity context, ListView listview) {

  // save the activity/context ref
  _context = context;

  // bind this model (and cell renderer) to the listview
  listview.setAdapter(this);

  // load some data into the model
  {
    String zip = "12345";
    Hashtable<String, String> wd = new Hashtable<String, String>();
    wd.put("temperature","30F");
    wd.put("humidity","50%");
    wd.put("icon","12");
    _data.put(zip, weatherdata);
  }

  Log.i(getClass().getSimpleName(), "loading data set, creating list model, and binding to listview");

}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// implement ListAdapter - how big is the underlying list data, and how to iterate it...
// the underlying data is a Map of Maps, so this really reflects the keyset of the enclosing Map...
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/** returns all the items in the {@link #_data} table */
public int getCount() {
  return _data.size();
}

/** returns the key for the table, not the value (which is another table) */
public Object getItem(int i) {
  Object retval = _data.keySet().toArray()[i];
  Log.i(getClass().getSimpleName(), "getItem(" + i + ") = " + retval);
  return retval;
}

/** returns the unique id for the given index, which is just the index */
public long getItemId(int i) {
  return i;
}
Notes on the code:
  1. The ListView.setAdapter() gets called to bind it to this weather data list adapter.
  2. In the constructor, some dummy data is created and loaded into the model. If you want to add data once the list has been constructed, you have to get the data from some where, and then call notifyDataSetChanged() on the adapter so that it can notify it’s ListView that it’s time to refresh the view. Make sure to call this notify method in the UI Thread using UIThreadUtilities.runOnUIThread(..) method. If you’re getting this data from a service or another background thread, you want to make sure and put the model update event in the UI thread so that there won’t be any UI hiccups.
  3. The getCount(), getItem(int) and getItemId(int) are 3 methods that allow the ListView to get visibility into your underlying data structure. These 3 accessor methods let the ListView know how big your list is, and how to access an Object that represents a row of data from your underlying model, given the selection index. These 3 methods provide a way for an index to be dereferenced into a row of data, which will then be renderered by the cell renderer. Just make sure when you’re writing your cell renderer that it matches up with these accessors.
Before we jump into the cell renderer code, let’s look at list selection methods that we have:
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// handle list selection
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/**
 * called when item in listview is selected... fires a model changed event...
 *
 * @param index index of item selected in listview. if -1 then it's unselected.
 */
public void setSelected(int index) {

  if (index == -1) {
    // unselected
  }
  else {
    // selected index...
  }

  _selectedIndex = index;

  // notify the model that the data has changed, need to update the view
  notifyDataSetChanged();

  Log.i(getClass().getSimpleName(),
        "updating _selectionIndex with index and firing model-change-event: index=" + index);

}
Notes on the code:
  1. This list selection code is tied to the selection listener that’s attached to the ListView in some of the code from previous paragraphs. The reason to have the selection update the list model is because my cell renderer displays different information depending on whether a cell is currently selected or not.
  2. The notifyDataSetChanged() method is called in the UI thread, in this case, since the setSelected(int) method gets called from a listener that’s attached to the ListView.
Finally, here’s the cell renderer code:
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// custom cell renderer
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@Override public View getView(int index,
                              View cellRenderer,
                              ViewGroup parent)
{

  CellRendererView cellRendererView = null;

  if (cellRenderer == null) {
    // create the cell renderer
    Log.i(getClass().getSimpleName(), "creating a CellRendererView object");
    cellRendererView = new CellRendererView();
  }
  else {
    cellRendererView = (CellRendererView) cellRenderer;
  }

  // update the cell renderer, and handle selection state
  cellRendererView.display(index,
                           _selectedIndex == index);

  return cellRendererView;

}
Notes on the code:
  1. When you override this method, you are telling the ListView that you want to use your own widget/View to render a row of data in your underlying data model.
  2. If the cellRenderer parameter is null, that just means that it’s time to create a new “rubber stamp” object to render each row. This object is then returned at the end of this method (for use in subsequent method calls to display different rows).
Here’s the CellRendererView class, which itself is just a TableLayout container that renders the weather data Hashtable:
/** this class is responsible for rendering the data in the model, given the selection state */
private class CellRendererView extends TableLayout {

  // ui stuff
  private TextView _lblName;
  private ImageView _lblIcon;
  private TextView _lblDescription;

  public CellRendererView() {

    super(_context);

    _createUI();

  }

  /** create the ui components */
  private void _createUI() {

    // make the 2nd col growable/wrappable
    setColumnShrinkable(1, true);
    setColumnStretchable(1, true);

    // set the padding
    setPadding(10, 10, 10, 10);

    // single row that holds icon/flag & name
    TableRow row = new TableRow(_context);
    LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(row);

    // fill the first row with: icon/flag, name
    {
      _lblName = new TextView(_context);
      LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(_lblName);
      _lblName.setPadding(10, 10, 10, 10);

      _lblIcon = AppUtils.createImageView(_context, -1, -1, -1);
      LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(_lblIcon);
      _lblIcon.setPadding(10, 10, 10, 10);

      row.addView(_lblIcon);
      row.addView(_lblName);
    }

    // create the 2nd row with: description
    {
      _lblDescription = new TextView(_context);
      LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(_lblDescription);
      _lblDescription.setPadding(10, 10, 10, 10);
    }

    // add the rows to the table
    {
      addView(row);
      addView(_lblDescription);
    }

    Log.i(getClass().getSimpleName(), "CellRendererView created");

  }

  /** update the views with the data corresponding to selection index */
  public void display(int index, boolean selected) {

    String zip = getItem(index).toString();
    Hashtable<String, String> weatherForZip = _data.get(zip);

    Log.i(getClass().getSimpleName(), "row[" + index + "] = " + weatherForZip.toString());

    String temp = weatherForZip.get("temperature");

    String icon = weatherForZip.get("icon");
    int iconId = ResourceUtils.getResourceIdForDrawable(_context, "com.developerlife", "w" + icon);

    String humidity = weatherForZip.get("humidity");

    _lblName.setText("Feels like: " + temp + " F, in: " + zip);
    _lblIcon.setImageResource(iconId);
    _lblDescription.setText("Humidity: " + humidity + " %");

    Log.i(getClass().getSimpleName(), "rendering index:" + index);

    if (selected) {
      _lblDescription.setVisibility(View.VISIBLE);
      Log.i(getClass().getSimpleName(), "hiding descripton for index:" + index);
    }
    else {
      _lblDescription.setVisibility(View.GONE);
      Log.i(getClass().getSimpleName(), "showing description for index:" + index);
    }

  }

}
Notes on this code:
  1. If a row/cell in the ListView is selected, then the humidity is displayed in a 2nd row of the TableLayout, otherwise it’s not. This is why a selection listener had to be attached, and model update events had to be fired, etc.
  2. Most of this code is just leveraging a TableLayout to display weather data.
  3. The icons are from weather.com, and are in their format… their forecasts have a 2 digit number that represents the icon file name that depicts current conditions… this is loaded using the ResourceUtils class, from R.drawable.*. This class is included in AndroidUtils.zip.
You can download LayoutUtils, AnimUtils and other helpful classes in AndroidUtils.zip.




Tuesday, March 6, 2012

New Android Project

The first steps of configuring Eclipse to create android applications and the steps involved in creating a first android project are very well described in this openframeworks.cc site. Hence I am not repeating the same here.

Monday, March 5, 2012

Basic Fundamentals | Android for Beginners (Part 1)


Fundamentals | Android Tutorial for Beginners (Part 1)
This is Part 1 of a series of articles I plan to write to simplify the new paradigms introduced by the Android platform for developers.
The part 1 will only define and introduce the fundamental building blocks of Android. Later articles will provide sample code focusing on one aspect at a time, more to drive home to concept than to show any great programming skills.

From a developer's perspective, the fundamental building blocks / components of Android are:
1. Activities
2. Services
3. Broadcast Receivers
4. Content Providers.

The means of communication between the above mentioned components is through
1. Intents
2. Intent Filters

The User Interface elements are by using what are called:
1. Views
2. Notifications

Now, having broadly classified the basics, I would like to give a simple definition for each of them, before we can appreciate the need for each of them.

Activity is the basic building block of every visible android application. It provides the means to render a UI. Every screen in an application is an activity by itself. Though they work together to present an application sequence, each activity is an independent entity. (Overall it’s like a from page )

Service is another building block of android applications which does not provide a UI. It is a program that can run in the background for an indefinite period.

Broadcast Receiver is yet another type of component that can receive and respond to any broadcast announcements.

Content Providers are a separate league of components that expose a specific set of data to applications.( Basic use for handling data base )

While the understanding and knowledge of these four components is good enough to start development, the knowledge of the means of communication between the components is also essential. The platform designers have introduced a new concept of communication through intents and intent filters.

Intents are messages that are passed between components. So, is it equivalent to parameters passed to API calls? Yes, it is close to that. However, the fundamental differences between API calls and intents' way of invoking components is
1. API calls are synchronous while intent-based invocation is asynchronous (mostly)
2. API calls are bound at compile time while intent-based calls are run-time bound (mostly)

It is these two differences that take Android platform to a different league.

NOTE: Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.


One component that wants to invoke another has to only express its' "intent" to do a job. And any other component that exists and has claimed that it can do such a job through "intent-filters", is invoked by the android platform to accomplish the job. This means, both the components are not aware of each other's existence and can still work together to give the desired result for the end-user.

This dotted line connection between components is achieved through the combination of intents, intent-filters and the android platform.

This leads to huge possibilities like:
1. Mix and match or rather plug and play of components at runtime
2. Replacing the inbuilt android applications with custom developed applications
3. Component level reuse within and across applications
4. Service orientation to the most granular level, if I may say

Now that the concept of intent has been introduced, let me get down to a more formal definition of Intent.

Intent is a bundle of information, a passive data structure that holds an abstract description of the operation to be performed. (or in the case of broadcasts, a description of an event that has happened and is being announced).

There are 2 types of intents which I intend to detail in the next part of this series. Before winding up part 1, I would finally also give you a formal definition of Intent filters.

Intent filters are the means through which a component advertizes its own capabilities to handle specific job/operations to the android platform.

Saturday, February 18, 2012

What is Variables...


    Variables are the key to any program. There are variables called registers inside every CPU (Central Processing Unit). Every program ever written uses some form of variables. Believe it or not, the way you use variables can significantly impact your program. This section is a very simple introduction to what variables are, and how they're used in programs.

    Usually, a variable implies a memory location to hold one instance of one specific type. What this means is that if there is an integer variable, it can only hold one integer, and if there is a character variable, it can only hold one character.
    There can be many different types of variables, including of your own type. A sample declaration for different variable types is given below.
boolean t;
byte b;
char c;
int i;
long l;
    I believe the above is straight forward, and doesn't need much explanation. Variable 't' is declared as boolean type, and 'b' as of byte type, etc.
    The above variables are what's know as primitive types. Primitive types in Java means that you don't have to create them, they're already available as soon as you declare them. (you'll see what I mean when we deal with Objects) It also means that there is usually some hardware equivalent to these variables. For example, an int type, can be stored in a 32 bit hardware register.
    The other types of variables are instances of classes or Objects. Java is a very Object Oriented language, and everything in it, is an object. An object is an instance of a class. Your Java programs consist of classes, in which you manipulate objects, and make the whole program do what you want. This concept will be familiar to you if you've ever programmed C++, if not, think of objects as structures. An example of a simple class would be:
public class pSimpleObject{
    int i;
    public pSimpleObject(){
        i = 0;
    }
    public int get(){
        return i;
    }
    public void set(int n){
        i = n;
    }
}
    As you can see, first we specify that the class is public, this means that it can be visible to other objects outside it's file. We later say that it's a class, and give it's name, which in this case is: pSimpleObject. Inside of it, the class contains an integer named 'i', and three functions. The first function named pSimpleObject(), is the constructor. It is called every time an object is created using this class. The set() and get() functions set and get the value of 'i' respectively. One useful terminology is that functions in objects are not called functions, they're called methods. So, to refer to function set(), you'd say "method set()."   That's all there is to objects!
    The way you declare a variable, or in this case, an object of that class, is:
pSimpleObject myObject;
myObject = new pSimpleObject();
or
pSimpleObject myObject = new pSimpleObject();
    The first example illustrates how you declare an object named myObject, of class pSimpleObject, and later instantiate it (a process of actual creation, where it calls the object's constructor method). The second approach illustrates that it all can be done in one line. The object does not get created when you just declare it, it's only created when you do a new on it.
    If you're familiar with C/C++, think of objects as pointers. First, you declare it, and then you allocate a new object to that pointer. The only limitation seems to be that you can't do math on these pointers, other than that, they behave as plain and simple C/C++ pointers. (You might want to think of objects as references however.)
    Using variables is really cool, and useful, but sometimes we'd like to have more. Like the ability to work with hundreds or maybe thousands of variables at the same time. And here's where our next section starts, the Arrays!

Basic Idea of Programming


Welcome to Android Programming. This document was created with an intent to show people how easy Android really is, and to clear up a few things I've missed in the previous release of the document.
    This is a growing document; as new features are added to the language, new techniques are discovered or realized, this document shall be updated to try to accommodate them all. If you have suggestions, or requests, (or spelling/grammar errors) just e-mail them, and I'll try to add the suggested topics into the subsequent release. Because this document is changing so much, I've decided to implement a version number. This release is: v2.2.11, updated: Feb 18th, 2012.