Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 240 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access values from Android "strings.xml" in Kotlin

#1
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)
}
}
Reply

#2
If you need to access the entire array:

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

#3
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>
Reply

#4
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(...)`.)
Reply

#5
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
Reply

#6
I used it for titlebar, but my app crashed.

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

#7
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"
Reply

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

#9
if you're accessing it outside oncreate then

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

#10
[@Robin's answer][1] 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)`


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through