Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: getString(R.string) in static method

When programming for Android sometimes you have to use static methods. But when you try to access you resources in a static method with getString(R.string.text) you'll get an error. Making it static doesn't work.

Does anyone knows a good way around this? The resource files in Android are very helpful for creating things in different languages or making changes to a text.

like image 292
Saren Inden Avatar asked Sep 29 '10 14:09

Saren Inden


People also ask

What is@ string in Android studio?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

What does Getstring do exactly?

Return a localized string from the application's package's default string table.

In which folder can you find the string resource file strings XML?

The XML resource files containing localized strings are placed in subfolders of the project's res folder.


2 Answers

One way or another, you'll need a Context for that... For static methods this probably means you need to pass along a Context when calling them.

like image 196
benvd Avatar answered Sep 17 '22 20:09

benvd


You could use Resources.getSystem().getStringArray(android.R.array.done);

like image 44
Prabin Byanjankar Avatar answered Sep 19 '22 20:09

Prabin Byanjankar