Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.