85

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)
    }
}
3
  • 9
    Just like in Java. Commented Jul 2, 2017 at 13:59
  • You've already referenced layouts and ids in your example. Also keep in mind that you can convert java files to kotlin using ctrl + shift + alt + k. There is also extensive documentation on methods available on kotl.in Commented Jul 3, 2017 at 1:54
  • thanx alot for help.. Commented Sep 24, 2017 at 15:34

11 Answers 11

136

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(...).)

Sign up to request clarification or add additional context in comments.

6 Comments

This could produce null
@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.
I tried to use this function, Android Studio couldn't even find it
@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 yeah i figured that out after writing my comment, was trying to do it in an object class, just wasn't explicitly stated
|
31

if you're accessing it outside oncreate then

    Resources.getSystem().getString(R.string.btn_yes)  

1 Comment

Only works for system resources, won't get application resources.
11

If you need to access the entire array:

val data= resources.getStringArray(R.array.array_id)

Comments

9

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

Comments

3

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

Comments

2

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

Comments

2

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

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

Comments

2

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 Developers documents

Comments

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>

1 Comment

Doesn't this return the integer id of that XML string, rather than the string content itself?
0

I used it for titlebar, but my app crashed.

private var title: String = getString(R.string.profile)

1 Comment

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.
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"

1 Comment

That's in XML files. But what about in Kotlin files?

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.