Search This Blog

Friday 1 February 2013

Its been a long time

Good day to you all.

It has been a very long time.  I've been busy, at work and then on holiday with my wife and son.  I am now back and hopefully some sort of normality can resume.  My request for help in the last post was met by deathly silence.  I have now brought my new laptop and can hopefully put it to good use.  It comes with Windows 8 and I may look at writing some Windows 8 apps as well - which may lead to me renaming this blog.

I've not done much Android development for a while but have gone through the pain of having to set up my development environment on my new laptop.  While doing this one of the things I realised was that some of my earlier posts are now out of date.  For example you no longer have to install Eclipse and the Android SDK individually, you can download as a pre-configured bundle.  Take a look at the following link for more details:

http://developer.android.com/sdk/index.html

I also had to download the Admob SDK which I use to monetize my app. If you are interested in making some money by monetizing your apps then check out the following link:

http://www.google.com/ads/admob/

It took me a while to get my Maths Test App working but I am now up and running (For those of you not familiar with my Maths Test App - here comes the standard plug  ->  "Search Google Play for Sundev Pabla and download the best, the greatest, the most awesome educational app on the market ; -)").

The other thing that I have downloaded to my new laptop is GIT, this is a configuration control tool for software.  I have got GIT linked to an online repository which is free from Assembla.  My code is stored in the online repository.  To learn more about GIT try googling it and for more info on Assembla repositories check out the following link:

https://www.assembla.com/features/git

I think that is enough for today, until next time all the best.

Friday 7 December 2012

Help Me

I could use your help in a number of ways.  

Firstly my MathsTest app is now pretty much complete (Find it by searching 'Sundev Pabla' on Google Play), I know it could be more polished and there are a number of improvements that could be made - remember its a first app and the idea was to learn Android development.  I want to start on a new project now- something new that will help me learn new Android skills that I can share with you via this Blog.  So please let me have your ideas for Apps that you think would be useful or would allow me to learn new Android skills.

The other way in which I could use your help is suggestions on what the best laptop for me to buy is. My expertise lies in writing software, at work I don't need to worry about hardware, the company buys it and the IT department maintain it.  At home I am finding that my 6 year old DELL Inspiron 6400 is getting slow and struggles when running Eclipse and the Android Emulator.  So please advise me on what the best bit of hardware for me is.  My requirements and thoughts on what I need are as follows:
  1. Must be a laptop - so that I'm not tied do the desk and can work on the move
  2. As a desktop replacement I think it needs at least a 15'' screen
  3. I want to be able to write Android Apps and Java Applications using Eclipse and Windows Applications using Visual Studio.
  4. I want to be able to run a version control tool like GIT (This reminds me, I must write a post at some stage on version control tools like GIT and Subversion)
  5. In my view the hard drive does not need to be huge because I have a 2 Tb external hard drive and can also use Cloud storage
  6. I'm thinking of branching out into Windows Phone and Windows 8 apps as well as Android.  I was therefore considering getting a Touch Screen Laptop (If any of you have experience of using Window 8 or touch screen laptops then let me have your thoughts on whether this is a passing fad or the future of computing).
  7. I'm seriously considering the DELL Inspiron 15z, (The i7, 8Gb RAM variant).  What do people think - does anyone own one?
That all for today - Until next time Good Buy.

Tuesday 16 October 2012

Presenting data from a database to a user

We have spent the last few posts looking at the class which encapsulates all the database related activity.  In the case of my Maths Test App this class is the MathsTestDbAdapter class (By the way my Maths Test App can be downloaded by searching Google Play for 'Sundev Pabla').

Today we will look at how you would use this class in the main part of your app.  You would use this class to get data from the database and put data into the database.  In my Maths Test App I use a database to store top scores.  In order to present top scores to the user I need to read top score info from the database and present it to the user.

In the Maths Test App I have a separate activity which I use for displaying scores to the user.  In the OnCreate( ) function of this activity I instantiate an instance of the MathsTestDbAdapter class and then call the open function on the instance.


        mDbHelper = new MathsTestDbAdapter(this);
        mDbHelper.open();

I can now use the mDbHelper object to call the get data functions in the MathsTestDbAdapter class.  I do this in the fillData function of  my ViewScoreActivity class.  The code for the fill data function is shown below:


    private void fillData()
    {

    Cursor topScoreCursor = mDbHelper.fetchScoresBasedOnSpecifiedGameLevelAndType(this.getResources().getStringArray(R.array.gameLevelArray)[gameLevelSpinner2.getSelectedItemPosition()],this.getResources().getStringArray(R.array.gameTypeArray)[gameTypeSpinner2.getSelectedItemPosition()]);
   
    startManagingCursor(topScoreCursor);
       
    tvInitial1 = (TextView) findViewById(R.id.initials1);
    tvScore1 = (TextView) findViewById(R.id.score1);
    tvInitial2 = (TextView) findViewById(R.id.initials2);
    tvScore2 = (TextView) findViewById(R.id.score2);
    tvInitial3 = (TextView) findViewById(R.id.initials3);
    tvScore3 = (TextView) findViewById(R.id.score3);
    tvInitial4 = (TextView) findViewById(R.id.initials4);
    tvScore4 = (TextView) findViewById(R.id.score4);
    tvInitial5 = (TextView) findViewById(R.id.initials5);
    tvScore5 = (TextView) findViewById(R.id.score5);
   
    // Will use switch case without breaks to populate table
    topScoreCursor.moveToFirst();
    switch (topScoreCursor.getCount())
    {
    case 5:
    tvInitial5.setText(""+topScoreCursor.getString(1));
    tvScore5.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 4:
    tvInitial4.setText(""+topScoreCursor.getString(1));
    tvScore4.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 3:
    tvInitial3.setText(""+topScoreCursor.getString(1));
    tvScore3.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 2:
    tvInitial2.setText(""+topScoreCursor.getString(1));
    tvScore2.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 1:
    tvInitial1.setText(""+topScoreCursor.getString(1));
    tvScore1.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    break;
    }
    }

The first thing I do in the fillData( ) function is call the fetchScoresBasedOnSpecifiedGameLevelAndType( ) function.  This returns a Cursor object which contains the data extracted from the database.

I then call startManagingCursor( ) function and pass in the cursor object.  This means that Android will look after the Cursor life cycle for me and I don't need to worry about things like closing the cursor when the activity is destroyed.  The moveToFirst( ) function on the Cursor object is used to move to the first data record returned by performing the query on the database.  In the switch statement I then use the Cursor to populate TextViews with data from the database.  For each record the first column or array element is a unique id, the second column is the players initials and the third column is the score.  To move to the next record you call moveToNext( ) on the cursor object.

Hopefully you can follow the code in the fillData( ) function, this is easy for me to say because I wrote the code and it may not be as self explanatory as I thing - so if you have got any questions then please get in touch.  I will leave it here for today, all the best until next time.

Tuesday 2 October 2012

Reading and Deleting data from an Android SQL database

To date we have looked at setting up the SQL database and creating the schema and also how you go about adding data to the database.  Today we will look at reading data from the database.

Reading data from the database

In my Maths Test App (Download this by searching Google Play for 'Sundev Pabla'), I provide two functions in the MathsTestDbAdapter class which allow data to be read from the database.  The functions return a Cursor object which contains the data returned by performing an SQL query on the database.  The code for the two functions is shown below.  The first function returns all the top scores in the database, whereas the second function uses arguments passed in to the function to filter the data and return a subset of the top scores.

    public Cursor fetchAllScores()
    {
    // This function returns data for all scores in the database in ascending order
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_USER_INITIALS,KEY_SCORE}, null, null, null, KEY_SCORE + " ASC", null);
    }


    public Cursor fetchScoresBasedOnSpecifiedGameLevelAndType(String level, String type)
    {
      return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_USER_INITIALS,KEY_SCORE}, KEY_LEVEL + " = " + "'" + level + "'" + " and " + KEY_GAME_TYPE + " = " + "'" + type + "'" , null, null,null,KEY_SCORE + " ASC", null);
    }

We will look at the Cursor object in a subsequent post, possibly when we look at presenting data read from a database to the user (This will probably be the next post).  Today we will focus on the mDB.query function used to read data from the database in the above two functions.

The first argument to the mDb.query function tells the function which table you want to query, the second parameter is a list of columns you would like returned, the third parameter is a SQL WHERE clause (without the WHERE) specifying which data records you would like to get from the database - passing in null returns all the records.  The next three arguments I don't use and simply pass in null.  I will leave it as an exercise for yourselves to find out how setting these parameters would affect the data returned by the database.  The seventh argument allows you to specify how you would like the data ordered.  I am ordering the data in Ascending order of Score -  so the first record will be the lowest score and the last will be the highest.  The last argument which I do not use, would allow you to specify a limit on the amount of data returned.

Deleting data from the database

Deleting data from the database is relatively simple.  In my Maths Test App I provide a function for deleting data from the database.  This is needed because I only store the top 5 scores, so when a score falls out of the top 5 it is deleted.  The function takes a rowId for the row of data to be deleted.  So in the main part of my application code I would have to first read data from the database, determine which row I want to delete and then call the delete function below using the rowID as an argument to the function call.  In the delete function below, the first argument is the table from which I want to delete data. The second argument is an SQL WHERE clause without the WHERE, it is used to specify the data I wish to delete.  The third argument I do not use - again I will leave it as an exercise for you to find out what it does and how it might be used.


    public boolean deleteScore(long rowId)
    {
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
I think thats probably enough for today - so until next time good bye.

Friday 14 September 2012

General Comment

This is a general comment for any body who has stumbled on this Blog. This Blog is for people who wish to write their own Android Apps - the tools for doing this are free assuming you already own a PC or laptop.

Start with the first post in this Blog (A Welcome from yours truly) and work through the posts which effectively form a tutorial. Also download my Android App by searching Google Play for 'Sundev Pabla'. Having the App on your phone will be useful as you work through the posts in the blog. If you have any questions then please get in touch by using the comment feature of the blog.

Good luck

Adding data to an SQL database


In the last post we looked at setting up the schema for an SQL database and creating the database.  Today we will look at how data would be added to the database.

As usual we will be using my Maths Test App as the basis of examples, so if you have not downloaded it, do this now (This can be done by searching Google Play for 'Sundev Pabla').

Data is added to the SQL database by creating an instance of the ContentValues class and populating the class with the data you want to put in the database (the ContentValues class provides the 'put' method for this purpose).  Finally the insert method on the database is called and the name of the Database table and the instance of the ContentValues class are passed in as arguments of the insert function.

Below is the code I use to add a top score to the SQL database in my maths test app.  This addTopScore function is a method of the MathsTestDbAdapter Class we discussed in the previous post.  You should be able to see that I create an instance of the ContentValues class which is provided by the Android API (So you will need the following import statement at the top of file in which this method is implemented):

import android.content.ContentValues;

I then 'put' the data I want to store in the database into the object I instantiated from the ContentValues class.  Finally I call the insert method on the database object and pass in the name of the table where I want the data stored along with the data I want stored.  The insert function returns a long which is a unique identifier for the row just stored.

 public long addTopScore(String initials,Double score)
    {
    // This function adds a high score to the database
    // The initials and score of the high scorer are passed in, info
    // about the game level and type are obtained from the preset user
    // preferences
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_USER_INITIALS, initials);
    initialValues.put(KEY_SCORE, score);
    initialValues.put(KEY_LEVEL, cUserPreferences.gameLevel);
    initialValues.put(KEY_GAME_TYPE, cUserPreferences.gameType);
   
    return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

I  think that is enough for today, as usual if you have any questions please get in touch via the blog.

Friday 7 September 2012

Adding an SQL lite Database to Android Apps

Adding an SQL lite database to your applications is surprisingly easy.  I was able to reuse code from the standard notepad tutorial for Android developers.  The notepad tutorial can be found at the following url:

http://developer.android.com/training/notepad/index.html

In this tutorial all the database related tasks are encapsulated in a class called NotesDbAdapter, this class was fairly generic and it was easy to modify it to suit my own purposes.  I named my own class MathsTestDbAdapter.java (To make it easier to follow this blog, download my Android App - Maths Test, locate it by searching Google Play for 'Sundev Pabla').

The MathsTestDbAdapter class encapsulates all the database related activity such as defining the database schema (Schema refers to the definition of tables in the database and the columns making up the tables), creating the database and handling the database activities known as CRUD (Create, Read, Update and Delete).

We will look at each of the CRUD activities in subsequent posts.  Today we will have a quick look at defining the database schema and creating the database.  The database is created by executing an SQL command.  I store the SQL command in a String called DATABASE_CREATE.  The code for setting up my SQL command for creating the database is shown below:

// String variables to describe the schema of the database
private static final String DATABASE_NAME = "Maths_Test_Db";
private static final String DATABASE_TABLE = "Performace_Stats";
private static final int DATABASE_VERSION = 1;

private static final String TAG = "MathsTestDbAdapter";
public static final String KEY_ROWID = "_id";
public static final String KEY_USER_INITIALS = "initials";
public static final String KEY_SCORE = "score";
public static final String KEY_LEVEL = "level";
public static final String KEY_GAME_TYPE = "type";

// Store an SQL command for creating the database in a string variable called
// DATABASE_CREATE
    private static final String DATABASE_CREATE ="create table " + DATABASE_TABLE + " ("
    + KEY_ROWID + " integer primary key autoincrement, "
    + KEY_USER_INITIALS + " text not null, "
    + KEY_SCORE + " real not null, "
    + KEY_LEVEL + " text not null, "
    + KEY_GAME_TYPE + " text not null);";

From the code you should be able to determine the schema of my Database.  My Database has 1 table called Performance_Stats.  The table contains 5 columns.  The columns are a unique row id column, a column to hold the initials of a high scorer, a column for the high score, a column to store the game level which the high score relates to and finally a column to store the game type being played when the high score was obtained.

I think this is enough for today.  If I was you I would now download the NotePad tutorial from the link above and have a look at the schema for the database in the NotePad tutorial.  You could also have a look at the functions for performing the CRUD operations - this will make it easier to follow subsequent posts.  If you do download the NotePad tutorial then remember the source file you want to be looking at is called NotesDbAdapter.java.

All the best until next time, enjoy your android journey.