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:

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

<strings>

<string id="AppName">Steps Carousel</string>

<string id="TotalSteps">Total</string>

<string id="ActiveSteps">Steps</string>

<string id="StepsToDo">To Go</string>

<string id="GoalPercentage">Goal (%)</string>

<string id="carouselSecondsTitle">Show next item in carousel every X seconds</string>

<string id="showTotalTitle">Show Total Steps (today's steps)</string>

<string id="showStepsTitle">Show Steps of the current activity</string>

<string id="showStepsToGoTitle">Show Steps left to reach goal steps of the day</string>

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.

Monkey

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

functioncompute(info){

varactivityMonitorInfo=getActivityMonitorInfo();

if(timerRunning){

stepsRecorded=activityMonitorInfo.steps-stepsNonActive;

ticker++;

}

vartimerSlot=(ticker%(fields*carouselSeconds));//modulo the number of fields*number of seconds toshow the field