0

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?

4 Answers 4

2

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

0

Figured it out. All you need to do is create a string in the res value folder and then reference it

1 Comment

It took you 3 minutes to find a solution, maybe next time you should spend a bit more time in trying to figure something out by yourself instead of asking a question straight away. ;)
0

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);

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.