Tutorial: Define Application Properties

In the previous tutorials we learned how to do unit testing. We’re going to use that project as a starting base to add application properties on top, so you can use your project you made so far or download my project from github as a starting version. .

Application properties: Why?

Application properties allow you to make your project configurable by the user. The user enters the values for the parameters you have defined and in our connect iq program we work with what the user has provided us.

Defining the properties

The properties are defined within the resources folder, you can give it any name and store it anywhere in the resources folder structure, but I like to call that file properties.xml and store it in a folder called settings for clarity. We also have to define a new set of string resources for the titles, you can store this within the properties.xml or within the strings.xml file you already have. In this case I’ve added it to the strings.xml file (mainly because I added multiple language support to this project)
This is what it looks like:

Get the settings

Retrieving the settings is pretty straightforward via Application.getApp().getProperty(…). This will retrieve the application properties at initialization time. When you wish to let the user change the settings while the application is running then you also need to implement the onSettingsChanged() function.
All we have to do now is do something with the settings that we read and use them in the compute function.
You can download the full source code from github

That’s all…

… or is it?

Well if we run the unit tests we get an error….

That’s not good! We’ll see in the next tutorial how to fix this!

Feedback

Did you like this article?Questions?
  • Post in the comments section below!

Leave a Reply