Luc
Luc

Reputation: 17082

Cannot get resource from android application

I am building an Android app, and I have a problem to get resource from string.xml.

I need to have a URL in String.xml that will be used several times through the app.

I tried Resources.getText("my_url"), but this does not work. Eclipse complains when I use this.

Do you think this is the best way to do ?

Upvotes: 5

Views: 13198

Answers (2)

Joe Ho
Joe Ho

Reputation: 928

Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?

If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.

Or, you have to post Eclipse's complains.

Upvotes: 0

David Webb
David Webb

Reputation: 193824

What you probably want is:

String myUrl = getString(R.string.my_url);

The getString() method is in Context which means that it's available directly in your Activity class since Activity is a subclass of Context. (The getString() method is also in Resources but it's easier to call on it directly on your Activity.)

What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R class as a public static final int. You then use these IDs to reference the resources. Have a look in the gen folder in your Eclipse project and you'll find the R class in there.

Upvotes: 12

Related Questions