Reputation: 196
I know this is a simple question, but I can't figure out how to reference strings in android. for example
on this EditText, I don't want to hardcode the string hi, I want it to reference a string in the res folder. How do I do this?
Upvotes: 0
Views: 137
Reputation: 7394
Create a file strings.xml in res/values/ folder, and add a text in it like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="mystring">MyStringFromXML</string>
</resources>
then reference the text in layout as :
android:text="@string/mystring"
And in Java file do it like:
String string = getString(R.string.mystring);
Upvotes: 0
Reputation: 196
Figured it out. All you need to do is create a string in the res value folder and then reference it
Upvotes: 0
Reputation: 674
Suppose you have following string in xml.
<string name="string_one">My string</string>
You have to access this in code :
String str = resources.getString(R.string.string_one);
You can also used in xml where you have take EditText :
android:text="@string/string_one"
Upvotes: 2
Reputation: 16603
Create a file strings.xml
in res/values
folder, and add a text like
<string name="card">Card</string>
then reference the text in layout as android:text="@string/card"
Upvotes: 0