Saturday, January 30, 2010

Android Adapters: an introduction


An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

An AdapterView is a view whose children are determined by an Adapter.

Some examples of AdapterViews are ListView, GridView, Spinner and Gallery.


There are several types or sub-classes of Adapter:

ListAdapter: Extended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.


ArrayAdapter: A ListAdapter that manages a ListView backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.


CursorAdapter: Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work.


HeaderViewListAdapter: ListAdapter used when a ListView has header views. This ListAdapter wraps another one and also keeps track of the header views and their associated data objects.
This is intended as a base class; you will probably not need to use this class directly in your own code.


ResourceCursorAdapter: An easy adapter that creates views defined in an XML file. You can specify the XML file that defines the appearance of the views.


SimpleAdapter: An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list.


SimpleCursorAdapter: An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.


SpinnerAdapter: Extended Adapter that is the bridge between a Spinner and its data. A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.


WrapperListAdapter: List adapter that wraps another list adapter. The wrapped adapter can be retrieved by calling getWrappedAdapter().

Android applications: Components and Structure


Android extends the standard views/data model, providing a new model that is suitable for equipment activated at all times.

The structure of an Android application is defined as follows:



The file AndroidManifest.xml

This defines the components of the application and their relationships. It gives the permissions to application as to what it can do with users. It can also give permission to components of the application.



The views (Class android.view.View)

The interface of a program for Android is a tree of views.



Activity (android.app.Activity class)
This is something that the user can do, translated into program. It corresponds to a screen, but can have multiple views.



Intent (android.content.Intent class)
Describes an action which must be performed.



Service (android.app.Service )
Program that operates in background.



Content Provider (android.content.ContentProvider class)
Encapsulates data and provides them commonly to several programs.



Notification (android.app.NotificationManager and android.app.Notification classes)
Class which informs the user about what is happening.

Besides components, there are also resources that can be XML files, image files as jpeg, etc. These use the android.content.Resources interface and are stored in the res directory.





Components of an Android application

Each component is included in a list stored in the manifest file AndroidManifest.xml of each application.



Activity

An activity corresponds to a screen. If an application is composed of several screens, it has an activity for each screen. Each activity is a class that extends the base class Activity. It has a graphical user interface made of views, and it responds to events. When you change screen, a new activity is launched.
It can return a value. For example, if an activity can choose something, text, image, it returns what is chosen.

The graphical interface of an activity is described by a Layout:
- Full screen.
- Float: dialogue or alert.
- None. In this case it works in background and is invisible. It is maked visible by giving it a layout.

Note that the graphical user interface is described in XML as XUL and XAML.



Intent

The intents are the goals of applications and are made effective by a new screen. An intent is made up of an action and data that are URI.

Examples of actions: MAIN, VIEW, EDIT, PICK.
If one wants to see a card about a person, an intent is defined. The action is VIEW and the data is the URI which enables access to this card.

IntentFilters describes how the action should apply.

IntentReceiver is an object that responds to external events. It can operate in the application or it can start an application.

Example of intent, view a webpage: VIEW for action and for data http://www.linkToStuff.org.



Service

A service is designed to operate independently of the screen, thus of activities. The best example is the music player that can works while moving from one screen to another.



Content Provider

Data stored by a computer program, in the form of files or SQLite databases are private and may not be used by other applications.
But Content Provider may be used to share data among several applications. The interface ContentResolver is the interface that provides data to other objects.



Notification

The class android.app.Notification defines how an event must be notified to user: displaying an icon, changing state of a led, vibration, or others. While the class android.app.NotificationManager sends the message in the form so defined.

Thursday, January 28, 2010

Inspiration: 50 Android games in one video..



*

- homerunbattle 3d
- speed forge 3d
- radiant
- flight director
- Zen Table Tennis
- tank ace 1944
- sky force
- Shoot U!
- Armageddon Squadron
- Light Racer 3D
- gang wars
- Wave Blazer
- death ride
- head to head racing
- avatar
- Farm Frenzy
- light racer
- Family Guy Uncensored
- super boom boom
- toonwarz
- air hockey
- meteor
- what the doodle!?
- Raging Thunder
- the settlers
- lightup
- the game of life
- military madness
- bebbled
- jump santa jump
- diner dash
- brain challenge
- assassins creed
- space physics
- million dollar poker
- derek jeter 2009
- armored strike
- robo defense
- guitar hero
- air attack
- wpt texas hold'em
- de Blob
- papastacker
- spades
- spore
- tennis slam
- Iron Sight
- TETRIS
- tower bloxx
- mechanics touch
- TightRope Hero
- flying aces

Game Developers Conference 2010 organizers have announced that they are working together with Google "to celebrate and inspire the mobile and independent game development communities" by offering free Nexus One and Verizon Droid by Motorola phones to select attendees.

The newly announced offer, part of Google's outreach into the mobile phone space as it expands use of its Android operating system, is open to qualifying developers who register to attend GDC 2010 by February 4th, 2010.

The Game Developers Conference, as the world’s largest professional-only game developer event, has been at the center of the industry’s discussions on these topics.

It serves as home to major Summits such as the GDC Mobile/Handheld Summit and the Independent Games Summit, as well as the IGF Mobile competition to award handheld game innovation.

In recent years, the smartphone has become one of the most widespread and widely-used game platforms, and has proven particularly suited to independent developers experimenting with new and unusual gameplay.

As an official statement on the tie-in notes: "This makes conference attendees great potential developers of new content for phones using the Android operating system." Alongside the announcement, Google's Eric Chu has posted about Android's GDC 2010 presence on the official Android Developers weblog.

"At the GDC, we are constantly looking for ways to help the game development community learn and thrive. The mobile and independent game spaces having been providing so many of those opportunities for years now," said Meggan Scavio, event director of the Game Developers Conference. "We are so appreciative that we can better reach those goals by actually putting a new opportunity – Android-powered devices – into the hands of our attendees."

Early Bird rates for GDC 2010 end February 4. For more information about the 2010 Game Developers Conference, including the eight summits and the Android phone promotion, please visit the official Game Developers Conference website.

Sunday, January 24, 2010

Creating multiple sqlite database tables in Android



Most of the Android database examples you will find on the web will usually contain only one table to demonstrate the basic database concepts.

That's great, the only problem with this is that most non-trivial database implementations will contain more than one table.


The standard database creation string for a single table will probably look a lot like the below:


private static final String CREATE_TABLE_1 =
" create table " + table1 +
" (_id integer primary key autoincrement," +
" title text not null, body text not null);";



Which is called in your DB Adapter class like this:


@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_TABLE_1);
}


So what to do if you want to create more than one table?
You may do the below.. but will it work?
Note that this is one big string containing three separate create statements...


private static final String DATABASE_CREATE_MULTIPLE_TABLES =
" create table " + ITEMS_TABLE +
" (_id integer primary key autoincrement," +
" title text not null)" +

" create table " + TAGS_TABLE +
" (_id integer primary key autoincrement," +
" tagName text not null)" +

" create table " + LOCATIONS_TABLE +
" (_id integer primary key autoincrement," +
" locationName text not null, gpsCoOrds text);"
;


And then you try calling them in your DB Adapter class like so:


@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(DATABASE_CREATE_MULTIPLE_TABLES);
}


So you're trying to create the three tables in one call to db.execSQL.


This appears to compile and run successfully, you can even read and write to the FIRST table that is created, but..
..when you try to read or write to any other table you will see the dreaded

'Sorry! The application Kdkddfblah (process test.Kdkddfblah) has stopped unexpectedly. Please try again'

..error message.


Uh-oh.

If you debug your application, you might see references to syntax errors 'near create', and possible a reference that the table doesn't exist.

Hmm... What went wrong?

The answer is that sqlite, and therefore the db.exec method, only lets you execute one sql command at a time. We were trying to run three sql statements in one go in db.execSQL(DATABASE_CREATE_MULTIPLE_TABLES);.

So what you need to do to fix this is move each of the above table create statements into their own strings, like this (Note that this now creates three seperate strings, unlike above):


private static final String CREATE_TABLE_1 =
" create table " + table1 +
" (_id integer primary key autoincrement," +
" title text not null, body text not null);";

private static final String CREATE_TABLE_2 =
" create table " + TAGS_TABLE +
" (_id integer primary key autoincrement," +
" tagName text not null)";

private static final String CREATE_TABLE_3 =
" create table " + LOCATIONS_TABLE +
" (_id integer primary key autoincrement," +
" locationName text not null, gpsCoOrds text);";


.. and then use these strings in your onCreate method like below, this then works.


@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
db.execSQL(CREATE_TABLE_3);
}


If you find this doesn't work for you, try dropping all the tables in your database and try again, or give the database a different name, or different version number. The SQLiteOpenHelper seems to have some troubles registering that the database is to be changed. It finds a db with the same name and version number, goes 'meh' and doesn't look to see if the structure is different at all.

You can also pull the sqlite db file right off your device (or emulator) by going into the DDMS perspective in Eclipse (Window menu\ Open perspective \ Other \ DDMS), navigating to the database file which will probably be at \data\data\*Your Application Name*\databases.

There's a 'pull file' button on the top left as seen highlighted below:



.. You can then open the DB in your favourite sqlite manager (I like Sqliteman) and play around. Can can also of course, push the file back to the device if you wish.




Monday, January 18, 2010

Psst..Remember that your layout files must have lowercase names..

Remember that your layout files must have lowercase names, or they won't show up in your autoComplete list of options after 'R.layout' in Eclipse when you try this :

setContentView(R.layout.test_db);


The file won't actually show up as having any errors in your package explorer (on the right by default in the IDE), but if you look down in the console (by default down the bottom), you'll see this:

Invalid file name: must contain only [a-z0-9_.]


You might see an error on your project name, but with all the folders and files it can be hard to track down the cause.

When you try to run your application you will see:

'Your project contains error(s) please fix them before running your application'


.. and it won't be happy until you delete the offending file, even if you're not actively referencing it in your code.



Hope that helps.. Happy coding!