-1

This is my string.xml. I wanted to call the string name url in the xml.How should i call it?

<resources>
<string name="app_name">FinalDemo</string>
<string name="action_settings">Settings</string>
<string name="url">"http://10.207.201.105/apexStore2/"</string>
</resources>

This is how i call. However i have been getting error. How should i call it?

String ipAddress = getApplicationContext().getString(R.strings.url);
Cheong Charlene
  • 275
  • 1
  • 2
  • 12

6 Answers6

1

Use this Code its work in your case

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);
Nitesh Pareek
  • 362
  • 2
  • 10
  • It say cannot resolve getApplicationContext(). Error in this part – Cheong Charlene Apr 27 '16 at 08:03
  • if you extends Activity then use this.getResources().getString(R.strings.url); else if you extends Fragment then use getActivity().getResources().getString(R.strings.url); – Nitesh Pareek Apr 27 '16 at 08:20
  • if you extends Activity then use this.getApplicationContext().getResources().getString(R.strings.url); else if you extends Fragment then use getActivity().getApplicationContext().getResources().getString(R.strings.url); – Nitesh Pareek Apr 27 '16 at 08:38
0

You should call it using

String ipAddress = getApplicationContext().getResources().getString(R.string.url);

See the related documentation here

Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
0

To learn more: http://developer.android.com/guide/topics/resources/string-resource.html

String url= getApplicationContext().getResources().getString(R.string.url);
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

Try This

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);
mdDroid
  • 3,135
  • 2
  • 22
  • 34
0

Try like this....

 String ipAddress =getApplicationContext().getResources().getString(R.string.url);

and for more information:- http://developer.android.com/guide/topics/resources/string-resource.html

Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
-1

in your question. you said in xml but

String ipAddress = getApplicationContext().getString(R.strings.url);

is a java code. In xml you can simply do it like this

 <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="@string/url" />

Also, in your error you should access your resources before the getString method

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);
Rashid
  • 1,700
  • 1
  • 23
  • 56