Android Preferences
(and Library Projects)

I’ve been battling with preferences in Android, on and off, since November 2013.

I wrote an app, Pit Board, and at the time split the preferences into two dialog boxes. A simple one for the message text and colour and an more complex one for animation styles and timings. I just created these in dialogs and thought nothing of it.

The animation styles I stored the style as an integer and the animation speed as a long value. I can then use ‘switch’ for the animation style and directly use the long value for the animation. Android preferences allow the storage of these types of variable.

With further development/reading I then wanted to replace my dialog box with a standard PreferenceFragment, which should reduce the work that I do as it automatically stores the values. A change listener can then be used in the activity.

BUT! The standard preference classes don’t have anything for an integer backed list or a way to store long values. Not only that, when setting default values in XML there is no support for long values. As a result I have had to write classes to handle those, and the long values one is a kludge as it stores the defaults as integers.

Library Projects

Obviously, having taken the time to write these I want to be able to share them across apps. At first I tried to put them in a different package (a parent to those that I was for my apps) but this causes problems when compiling. Lots of errors about not finding resources (the R class).

Reading more it seems that projects can be defined as libraries and these can be incorporated at compile time. I set about moving the preferences (and another useful class) to “utils” this morning on the train. Poor internet access can be very frustrating! I studied the examples in the TicTacToe app, but got nowhere. When I tried to compile the project it failed to find the classes that I’d moved to the utility library.

I worked on it on the way home and made the same amount of progress. Once home I googled further and found a useful post which held the secret line that fixed it!

I’d already updated the project, but I hadn’t included the “--library relative/path/to/library/project” bit. When I look at the files I can’t just see what it altered, but it compiles now. (I’d already added the library reference to project.properties and even fixed the capitalisation!)

Flattr this!