0

I have a code in Android like this;

public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"};

I wanna make this code like this;

public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"};

I tried too much code but it didn't work. What can I do about it? Thanks a lot.

2 Answers 2

1

@string resources are saved by there int references in R.java file. So, you can't use them directly. However, there is another way to do that.

You can use string-array in arrays.xml file. It would be like..

<string-array name="urls">
    <item>https://www.url1.com/</item>
    <item>https://www.url2.com/</item>
    <item>https://www.url3.com/</item>
</string-array>

In your Java Code, you can access it through following code.

public static final String[] URLS = context.getResources().getStringArray(R.array.urls);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use Resources to get strings from android resources.Try below

Resources res = context.getResources(); public static final String[] URLS = new String[]{res.getString(R.string.url_1), res.getString(R.string.url_2)};

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.