How can I access values from strings.xml in kotlin android?
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(p0: Vie
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>
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(...)
.)
If you need to access the entire array:
val data= resources.getStringArray(R.array.array_id)
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