Category: Android

When Eclipse won’t generate/locate

I’ve just come across a rather irritating habit of the ADT toolset for Eclipse and thought I’d share it.

If you get a red line under “R” in a reference to a layout, string or id, (e.g. the “R” in R.layout.main) then your problem could be one of the following:

  • You have ‘android.R’ as an import in your java class. This should not be there, remove it
  • You need to ‘clean’ your project by going to Project > Clean on the Eclipse menu bar
  • You do not have Eclipse set to ‘Build Automatically’. Do this by going to Project in the menu bar and ticking ‘Build Automatically’

My problem, however, was none of these. Eclipse just refused to generate

It turns out that if you have any errors in your xml files (strings.xml, layouts, etc.), then your file will not be generated and you will not be told this is the reason.

Make sure any errors in your xml files are fixed and your file should be generated automatically when you save.

SQLite timestamp comparison causes SQLiteException or error

When getting the “next” or “previous” entry in an SQLite table, I’ve been using the rowId so far and it’s been okay.

Recently, though, it’s not been that easy. For example, when a user imports a set of entries, some of which have an earlier timestamp than the current entries, they end up in the middle of the entries but their rowIds are newer and so are considered to be at the end of the list.

This causes weird navigation issues, so it’s best to stick with a single way to order entries.

Here is a sample of a SELECT statement that will get a rowId of the next entry in order of timestamp:

However, this causes an SQLite exception in Android/Java and other errors in other SQLite implementations. The problem is the space in the timestamp given. Whenever there is a space there should be quotation marks surrounding the piece of text.

So the timestamp should be “2010-09-15 13:15:20.250″, giving a SELECT statement that looks like this:

This selects one row where the timestamp is the next greatest than the given one, i.e. the one right after the given one. It should also be error-free, now that we’ve included the quotation marks.

Backing up, importing and restoring databases on Android

If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.

Below is a class called DbExportImport. Once you’ve set it up with the correct values, all you need to do is call exportDb(), importDb() or restoreDb() from your application to perform the necessary operations.

This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.

I have only left in a few comments, so for any clarification, leave a comment and I’ll make it a little clearer.

Updating files and folders to Android device with one click via adb

I like to keep an updated copy of certain files on my Android phone at all times, such as the latest copy of certain code so I can look at it when I’m on the train.

I’ve done it in quite a few different ways:

  • Mount SD card as a removable storage device via USB
  • Run an FTP server on my phone and transfer files over wifi
  • Bluetooth file transfer

But these are all tedious because they’re all manual and they take too long.

Then I realised that ADB push and ADB shell are run from the command line/terminal, which means I can run a batch script. (A batch script is a text file with commands written that all run one after another in the command line)

Now all I need to do is click the file on my desktop and the files get updated within a fraction of a second on my phone.

So here’s an example version of my batch file that automatically updates a folder on my phone:

‘rm -r’ delete the folder and everything in it.

‘mkdir’ creates a folder by that name.

‘push’ copies the folder contents from the first argument to the second argument.

Note for those running emulators on their computers: Add ‘-d’ after the word adb on each line to send the commands to a device and not an emulator.

All that’s left is to write it up in Notepad and save it as Filename.bat. After that, just double-click the file and the operation should run in a fraction of a second.

ADB ‘Push: Permission Denied’ on Android adb shell push

I just spent a little while trying to figure out why I was getting ‘push: permission denied’ when running:

I did some googling to no avail.

Then I remembered that pushing and pulling is not done in the shell, it’s just done with adb.

Thus, it should have been:

Which succeeds. I know it’s a silly error to make, but I thought I’d share this in case anybody else uses the adb shell push instead of regular adb push and can’t figure out why permission is denied.

How to change the refresh rate/interval of AdMob ads on Android

Once you’ve set up AdMob ads for your application, you might want to change how often they are refreshed.

If they are refreshed often, your users will see more ads when they’re on the same screen. This is good in that they see more ads, but can also be very annoying.

In the XML entry for your AdMob banner, just add this attribute to set your refresh rate to 60 seconds, as an example:

AdMob only allows you to have a refresh rate between 12-120 seconds

The complete XML element for the AdMob banner should look something like this: