Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Code challenge - All About @string Resources

Question:

In StringTest.java add a String variable named 'label' before the call to setText(). Use the getString(int id) method to set 'label' to the value you just added in strings.xml. The id will begin with 'R.string'.

Code:

String[] phoneNumberLabels = { "-home", "-work", "-mobile" };


Button saveButton = (Button) findViewById(R.id.saveButton);


saveButton.setText("SAVE");

Can anyone help me with this challenge? Thank you in advance

1 Answer

You're refering to:

Build a Blog Reader Android App > Rebuilding from Scratch > All About @string Resources

Challenge #2 of 5

The complete answer is:

String[] phoneNumberLabels = { "-home", "-work", "-mobile" };
String label = getString(R.string.saveLabel);
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setText("SAVE");

Let me know if you don't understand the question. Keep coding!

Sam Donald
Sam Donald
36,305 Points

In the instructions it says to use getString(int id) and that the id will start with R.string.

Given how you have shown it above isn't it getString(id int) ?

So the full question reads:

In StringTest.java add a String variable named 'label' before the call to setText(). Use the getString(int id) method to set 'label' to the value you just added in strings.xml. The id will begin with 'R.string'.

and we begin with

String[] phoneNumberLabels = { "-home", "-work", "-mobile" };

Button saveButton = (Button) findViewById(R.id.saveButton);

saveButton.setText("SAVE");

It first states:

In StringTest.java add a String variable named 'label' before the call to setText().

Our java:

String[] phoneNumberLabels = { "-home", "-work", "-mobile" };

Button saveButton = (Button) findViewById(R.id.saveButton);
String label;
saveButton.setText("SAVE");

The next (and technically final) part of the current challenge:

Use the getString(int id) method to set 'label' to the value you just added in strings.xml.

Our java:

String[] phoneNumberLabels = { "-home", "-work", "-mobile" };

Button saveButton = (Button) findViewById(R.id.saveButton);
String label = getString(R.string.saveLabel);
saveButton.setText("SAVE");

That's it!

If you read the challenge carefully it says Use the **getString(int id)** method to set 'label' to the value you just added in strings.xml It's just letting you know that the getString method takes an int id as a parameter. It's really just using the technical name of the method. As seen here in the Android developer documentation: http://developer.android.com/reference/android/content/res/Resources.html#getString(int)


Taken from the link above:

public String getString (int id)

Added in API level 1 Return the string value associated with a particular resource ID. It will be stripped of any styled text information.

Parameters

id    The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

Returns

String The string data associated with the resource, stripped of styled text information.

Throws

Resources.NotFoundException Throws NotFoundException if the given ID does not exist.