Skip to content

Android coding session 4

February 6, 2011

I now wanted to see if I could add hard coded values taken from strings.xml

1. I built and ran this tutorial This gave me the understanding of how to reference values from strings.xml file.

The key line of code was this
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter(this, R.layout.list_item, countries));

Which I changed in to
String[] cat = getResources().getStringArray(R.array.cats);
setListAdapter(new ArrayAdapter(this, R.layout.list_item, cat));

Then customised my list in Strings.xml code

Russian Blue
British Blue


Here is what it looked like

This was a really good exercise and I learnt about referencing an xml file (in using get resources and r)

2. Next I wanted to do some formatting and found this example of a table view

a. I built and ran this tutorial, and adapted the code

The values it was displaying where hard coded text with an integer.

b. I wanted to display string values. So, I added this code to strings.xml

Then I adapted the code to pull in the test value
String testVal = this.getResources().getString(R.string.testValue);
Then I adapted the code to pull in the test value
map.put("col2", "" + testVal);
On the app output, each row in the second column now had ‘test’ displayed instead of col_1_item_..

This proved that I could output xml values from Strings.xml

c. Next I wanted to display my cat breeds.
I added my cat breeds to strings.xml. But this was an array I was unable to get each value, but I could get the number of entries in the array (the length).
I added this to the java file
String[] cat = getResources().getStringArray(R.array.cats);
This passed the values from strings.xml to an array called cat and outputted the number of items in the cat array using the length method
map.put("col4", "" + cat.length);
On the apps output, each row in the forth column now had ‘6’ displayed instead of col_3_item_..

d. Next I needed to amend the for length of the loop, so changed the ‘for’ statement to
for(int i = 0; i < cat.length; i++){
This meant that there were only six rows outputted rather than 10 previously.

e. Next I wanted to display each cat breed, adding the array position [i] for each row
map.put("col3", "" + cat[i]);
On the apps output, each row in the third column now had different cat breeds displayed instead of col_2_item_..

In order to do this I needed to get my head around arrays, ‘for’ loops and ‘if‘ statements again, I found these sites very useful for my revision. and

3. Next, I wanted to see if I could add custom attributes and display the value.

In strings.xml, I added
I was unable to output the value and could not find any example code for this on the web and after a few hours I gave up on this approach, this was a real shame, as the handball.xml file used custom attributes.

4. I then looked at multiple arrays, and decided to create 4 arrays in strings.xml

Next, I amended the java code adding references to the new arrays
String[] homeTeam = getResources().getStringArray(R.array.homeTeam);
String[] homeScore = getResources().getStringArray(R.array.homeScore);
String[] awayTeam = getResources().getStringArray(R.array.awayTeam);
String[] awayScore = getResources().getStringArray(R.array.awayScore);

Updated the ‘for’ statement
for(int i = 0; i < homeTeam.length; i++){
Linked the new array to each column
map.put("col1", "" + homeTeam[i]);
map.put("col2", "" + homeScore[i]);
map.put("col3", "v " + awayScore[i]);
map.put("col4", "" + awayTeam[i]);

I also adjusted the column widths, padding in table.xml & main.xml

Next I changed the heading row names and the heading background colour
android:text=" Home Team"

Last but not least, I wanted to add some logic to the app, to show the winning team, with an asterisk.
I used an if statement to compare the values for the score, if the awayscore was more than the home score then it will put an asterisk next to the away team name, else the hometeam one then it would add an asterisk by the home team name

int result = homeScore[i].toString().compareTo(awayScore[i].toString());

if (result < 0){

HashMap map = new HashMap();
map.put("col1", "" + homeTeam[i]);
map.put("col2", "" + homeScore[i]);
map.put("col3", "v " + awayScore[i]);
map.put("col4", awayTeam[i] + "*");
}else {
HashMap map = new HashMap();
map.put("col1", "" + homeTeam[i]+ "*");
map.put("col2", "" + homeScore[i]);
map.put("col3", "v " + awayScore[i]);
map.put("col4", "" + awayTeam[i]);

Final screen shot of Handball results

So in the end my solution is not brilliant having 4 different arrays, this can lead to errors and also it doesn’t represent the handball.xml file that I started out with.

But I proved 3 things that you can added a rss feed , get a map to locate a place, and format string values to an android app. In the future I would like to combine this functionality in to a single app.

Finally I deployed the app to a fellow students android phone after I added the code below, found in this tutorial into the tag in AndroidManifest.xml


For my final project I am not sure if I will continue with android development, I found this task hard and painful at times, but quite rewarding in the end. Coding doesn’t bring out the best in my personality. This also makes me appreciate how complex app development is, and what would have gone in to developing my favourite iPhone apps.

After this course, I don’t want to go back to coding jobs, I won’t to get back into to main stream broadcasting, this also makes me question weather I should be developing mobile phone applications.

Useful android development sites

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: