First Love

The first phone I ever got was a Motorola Startac 70. This was back in 98 or so, a flip phone and I still remember it ever so clearly!

I’m pretty sure my next phone was an A1000, a pretty awesome phone. This was around the time that Motorola started releasing the razr which I was far from impressed with. I jumped ship to Sony Erricson and I went through a couple of those before heading over to the wonderful world of HTC. I wanted a smart phone to help with work and so on and I got the HTC HD (If I recall) which was a Windows phone.

While this make me uncomfortable, I was still more comfortable with this than a blackberry. I couldn’t get an iPhone for some reason that I cannot remember.

The phone was pretty laggy and I got pretty sick of it pretty quick. I then got myself my first android phone – an HTC Hero… Most people didn’t like the curvy bit at the bottom but I didn’t care. I was happy to be on the cutting edge. It was also a bit slow and laggy. but it was android and I loved it. It started a brand new love affair… Let me tell you more…

Continue reading

Android – Parcel data to pass between Activities using Parcelable classes

Passing data between activities on android is unfortunately, not as simple as passing in parameters. What we need to to do is tag these onto the intent. If the information we need to pass across is a simple object like a String or Integer, this is easy enough.

String strinParam = "String Parameter";
Integer intParam = 5;

Intent i = new Intent(this, MyActivity.class);
i.putExtra("uk.co.kraya.stringParam", stringParam);
i.putExtra("uk.co.kraya.intParam", intParam);

startActivity(i);

Passing in custom objects is a little more complicated. You could just mark the class as Serializable
and let Java take care of this. However, on the android, there is a serious performance hit that comes with using Serializable. The solution is to use Parcelable.

Continue reading

Android – Managing Global Configuration

The Problem

Accessing preferences / configuration / settings from Android is actually pretty straightforward as long as you are in an Activity. To read:

// PREFS_FILENAME = "nameOfPrefsFile";

SharedPreferences pref = getSharedPreferences(PREFS_FILENAME,
                              Context.MODE_PRIVATE);

String string = pref.getString("key", "default");
// 1 is the default if key isn't set
int intValue = pref.getInt("intKey", 1); 

// and so on

SharedPreferences is the key class. To write, you also need the SharedPreferences.Editor class, as follows:

// PREFS_FILENAME = "nameOfPrefsFile";
SharedPreferences pref = getSharedPreferences(PREFS_FILENAME,
                              Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString("key", "value");
editor.putInt("intKey", 5);  

// Until you call commit, the changes will not
// be written, so don't forget this step
editor.commit();

In general however, you will need access to settings in more than one activity and it seems a bit wasteful to get these bits littered through the application. Since I am lazy and like to write things just once, I  separated all the prefs stuff into one class called Settings.

Continue reading