Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 520
Luc Avatar asked Jan 27 '10 09:01

Luc


1 Answers

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.

like image 51
Dave Webb Avatar answered Oct 11 '22 09:10

Dave Webb