0

I plan on having a bunch of URLs specified in strings.xml, eg:

<string name="a1_url">https://en.wikipedia.org</string>

I would like to be able to open a URL (eg string name "a1_url") from an activity, but can't figure out how to reference the string name in Uri.parse. This current code works fine when you hardcode the URL:

Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse("https://en.wikipedia.org"));
                startActivity(i2);

My overall goal is to have many URLs as strings and the app will choose one for you at random, eg a1_url or a233_url.

2
  • You can get the value from string.xml with getString() method of Context. developer.android.com/reference/android/content/… Commented Jan 3, 2021 at 8:23
  • 1
    please don't include the android studio tag just because you're using it to develop your app, only use it if you're asking about a feature of the ide specifically Commented Jan 3, 2021 at 8:34

2 Answers 2

1

First, you need to load the string value from resources XML

String url = getString(R.string.a1_url);

Then use it in your intent

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, here's the end result: String url = getString(R.string.a1_url); Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
0
Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse(R.string.a1_url));
            startActivity(i2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.