Nathvi
Nathvi

Reputation: 196

How to use strings in android

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

Answers (4)

Shridutt Kothari
Shridutt Kothari

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

Nathvi
Nathvi

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

Kels
Kels

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

Axarydax
Axarydax

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

Related Questions