Ronnie
Ronnie

Reputation: 11198

How to properly reference the Strings.xml file, android

My string.xml has a string:

<string name="loginLocation">http://website.com/afile.php</string>

In my java file, I am trying to reference it like so:

url = new URL(R.string.loginLocation);

except I am getting the error:

The constructor URL(int) is undefined

I managed to get the error to go away by doing:

url = new URL(Integer.toString(R.string.loginLocation));

except when I make the call to it, I get a Protocol Error

I can do:

url = new URL("http://website.com/afile.php");

and it works fine, but I'd like to define it in the Strings.xml file. Any help is appreciated, thanks!

Upvotes: 2

Views: 2565

Answers (3)

Josh
Josh

Reputation: 6393

As simple as this:

this.getxt(R.string.btn_menu_logout);

What nobody mentions online is to put .this before gettxt() or getText():

Upvotes: 0

Ilya Saunkin
Ilya Saunkin

Reputation: 19820

getResources() will give you a lot of methods for accessing what's in your /res directory. Among the rest, the method you are looking for is getString(R.string....), which is also available directly from your Context (without the Resources object).

Upvotes: 0

Michael
Michael

Reputation: 54725

If you're trying to do it in a method of the Activity subclass, then do the following:

url = new URL(getString(R.string.loginLocation));

Upvotes: 3

Related Questions