72

How can I access values from the Android strings.xml using Kotlin?

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(p0: View?) {
        getToastCalled("")
        TODO("not implemented")
    }

    private fun getToastCalled(message: String) {
        TODO("not implemented")
    }

    var btn: Button? = null;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var tv_name=findViewById(R.id.tv) as TextView
        btn=findViewById(R.id.button) as Button
        tv_name.setText(KtTest().name);
        (btn as Button).setOnClickListener(MainActivity@this)
    }
}
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Dheeraj
  • 869
  • 1
  • 7
  • 13

11 Answers11

119

Accessing string values in Kotlin works exactly the same as it does in Java, just with Kotlin syntax:

val string: String = getString(R.string.your_string_id)

(This assumes that your code is inside an Android Activity, or a similar class that inherits from Context. If not, you need to get a Context instance from somewhere, and change it to myContext.getString(...).)

Robin
  • 3,682
  • 2
  • 22
  • 26
  • 1
    This could produce **null** – IgorGanapolsky Oct 23 '18 at 17:34
  • 7
    @IgorGanapolsky Well, yeah, technically it could, but that's a programming error or something's wrong with the build process if it does. This call returning null isn't something you could recover from in any meaningful way, so in Java, null is never checked here, and you luckily you don't need to in Kotlin either. – Robin Oct 23 '18 at 20:12
  • I tried to use this function, Android Studio couldn't even find it – Rowan Berry Apr 06 '20 at 02:31
  • 3
    @RowanBerry `getString` is defined on `Context`. My example code was written assuming you're inside an Android `Activity`, which inherits from `Context`, but if you need it somewhere else, you need to have a `Context` available somewhere. You need to change the example it to `myContext.getString(...)` in that case. (Disclaimer - haven't done Android development for a long time now, details might have changed) – Robin Apr 06 '20 at 13:32
  • @Robin yeah i figured that out after writing my comment, was trying to do it in an object class, just wasn't explicitly stated – Rowan Berry Apr 07 '20 at 00:34
  • Yeah, I guess it might trip others up as well. I've added the clarification to the answer, thanks! – Robin Apr 07 '20 at 14:45
22

if you're accessing it outside oncreate then

    Resources.getSystem().getString(R.string.btn_yes)  
Prat137
  • 249
  • 2
  • 6
11

If you need to access the entire array:

val data= resources.getStringArray(R.array.array_id)
piet.t
  • 11,718
  • 21
  • 43
  • 52
Dhivysh
  • 259
  • 3
  • 10
6

If it helps this is how I did it - you need the application context and also to import the R Class

applicationContext.getString(R.string.text_string)
import com.example.yourpackagename.R
Tonnie
  • 4,865
  • 3
  • 34
  • 50
2

To use strings.xml in my code I do the following:
textView.text = "${getString(R.string.my_text)}"

1

@Robin's answer almost worked for me, but if I don't add this.resources before the getString(R.string.tv) my app just keeps crashing.

So to access a string value from within the strings.xml file in Kotlin use:
this.resources.getString(R.string.tv)

Or simply:
resources.getString(R.string.tv)

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
1

you can access string value in kotlin by

  val string:String = resources.getString(R.string.variable_name)

or use

val string: String = getString(R.string.variable_name)

For more details, you can refer to the Android developer documents

1

In my recyclerview adapter, inside the ViewHolder class, I reference to string.xml like this;

binding.myText.setText(R.string.my_text)
0

Use the format - R.string.string_name

Reference it explicitly for example:

R.string.loadingText,

Given your XML file has the following as part of the resources:

<string name="loadingText">The actual text to be displayed</string>
zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33
tonderaimuchada
  • 215
  • 2
  • 6
  • Doesn't this return the integer id of that XML string, rather than the string content itself? – Mario Jul 12 '21 at 14:20
0

I used it for titlebar, but my app crashed.

private var title: String = getString(R.string.profile)
  • Separate the declaration and usage like private var title: String and in onCreate method or any other method where you want to access title = "${getString(R.string. – Sundeep Sharma Mar 23 '22 at 12:05
0

You can access string values simply through - "@string/variable_name".

For instance - if strings.xml contains entry like <string name="app_name">Tip Time</string>, then you can access it using -

"@string/app_name"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shubham Jain
  • 31
  • 1
  • 6