Android Shared Preferences Backed Up

I have been looking around for some way to back up the preferences in my Android app – just a simple serialization of the SharedPreferences object. Here are some code snips from my backup object that allowed me to get the job done:

private void importSharedPreferences()
{
  SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
  File myPath = new File(EXPORT_FILE_PATH);
  File myFile = new File(myPath, PREFS_FILE_NAME);
  if(myFile.exists())
	{
		BufferedReader i = new BufferedReader(new InputStreamReader(new FileInputStream(EXPORT_FILE_PATH + PREFS_FILE_NAME), "UTF8"));
		String line;

		while ((line = i.readLine()) != null)
		{
				String[] pair = line.split(":");

				SharedPreferences.Editor prefEdit = prefs.edit();

				if(pair[2].indexOf("Boolean")>-1)
				{
					prefEdit.putBoolean(pair[0], Boolean.parseBoolean(pair[1]));
				}
				else if(pair[2].indexOf("Integer")>-1)
				{
					prefEdit.putInt(pair[0], Integer.parseInt(pair[1]));
				}
				else if(pair[2].indexOf("Float")>-1)
				{
					prefEdit.putFloat(pair[0], Float.parseFloat(pair[1]));
				}
				else if(pair[2].indexOf("Long")>-1)
				{
					prefEdit.putLong(pair[0], Long.parseLong(pair[1]));
				}
				else if(pair[2].indexOf("String")>-1)
				{
					prefEdit.putString(pair[0], pair[1]);
				}

				prefEdit.commit();
			}
	}
}
public void exportSharedPreferences()
{
    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);

    File myPath = new File(EXPORT_FILE_PATH);
    File myFile = new File(myPath, PREFS_FILE_NAME);

    FileWriter fw = new FileWriter(myFile);
    PrintWriter pw = new PrintWriter(fw);

    Map<String,?> prefsMap = prefs.getAll();

    for(Map.Entry<String,?> entry : prefsMap.entrySet())
    {
    		pw.println(entry.getKey() + ":" + entry.getValue().toString() + ":" + entry.getValue().getClass());
    }

    pw.close();
    fw.close();
}

I removed all of the error handling and might have messed up the formatting a bit, but you get the idea. Also, I plan on moving the serialization to a XML format in the next few days instead of the janky colon-separated bit. Hope this is helpful to you!

Android Layout findViewById Returns Null

Every once in a while you run across a coding issue that has occurred in the past but you can’t remember the resolution. Tonight is one of those times and the issue was an Android Layout one.

I had a layout xml file with the following id on a TextView:

id=”@+id/foo”

In the OnCreate() method I set the content view:

setContentView(R.layout.my_main);

And then went to get the TextView as such:

TextView foo = (TextView) this.findViewById(R.id.foo);

The problem is, foo is always null.

The issue here is that you must use the correct schema for the id node, which I’m guessing is compiling correctly due to some past version compatibility “feature” in the Android code. Simply making the change to use android:id as shown below, made the null an issue of my past (again) and now I can move on to using the TextView any way I like.

android:id=”@+id/foo”

ToMarket Grocery Shopping List

OK, lets get this one out of the way early. I have a little shopping app on the Google Android platform. The idea started some time before the initial Android release and it’s first phone, the G1, when my wife was complaining about her Palm device being a dog. I told her about the impending Google mobile OS, and she said I should write a replacement for an App she liked, called HandyShopper. Without looking at the app, I interviewed her and wrote a set of requirements out and got to work. I had a free app in the market shortly after launch, and a paid one on the day they allowed paid apps. A couple of years have passed, and there is still a strong user base that enjoys the app and participates in the community over on http://ToMarketWeb.com. OK, enough of the self plugging, but if you find yourself with an Android device, check out ToMarket.