Android: How to Get String from Resources Using Its Name

Android: How do I get string from resources using its name?

The link you are referring to seems to work with strings generated at runtime. The strings from strings.xml are not created at runtime.
You can get them via

String mystring = getResources().getString(R.string.mystring);

getResources() is a method of the Context class. If you are inside a Activity or a Service (which extend Context) you can use it like in this snippet.

Also note that the whole language dependency can be taken care of by the android framework.
Simply create different folders for each language. If english is your default language, just put the english strings into res/values/strings.xml. Then create a new folder values-ru and put the russian strings with identical names into res/values-ru/strings.xml. From this point on android selects the correct one depending on the device locale for you, either when you call getString() or when referencing strings in XML via @string/mystring.
The ones from res/values/strings.xml are the fallback ones, if you don't have a folder covering the users locale, this one will be used as default values.

See Localization and Providing Resources for more information.

Android get string from resource?

You should try with getResources()

Returns a Resources instance for the application's package

String defaultText = getResources().getString(R.string.release_movie_message_now);

FYI

 getResources() is not recognize

Then you should add Context object before getResources()

Get a string resource from values folder in Android

To access Strings, you do as Berat said in his answer. From code using ID's (R.string.resName) and XML using the @string annotation: @string/resName to get the String.

However, when you use code (e.g. Java), you can't just use the ID. You also have to use Context.getString(R.string.resName); to get the actual String. So if you want to access the String from code and get it as a variable, you do this:

String res = context.getString(R.string.myStringResource);

I write context because it is a class in Context. You can, however, use Activity instances to get the String as well. If you are writing the code inside an activity, you can just write String res = getString(R.string.myStringResource);.

Activity means either extending Activity or AppCompatActivity (or a class that extends either on some level)

Android - How to get from string resources all the elements which contains two or more similar words on their names

But for this case i have to make it dinamically obtaining all the items which have a determinated word

You do not need to do it dynamically. The resources are known at compile time. A new set of resources will not appear magically in your app with a new prefix.

without a String array resource

A string-array resource would be the sensible way of doing it. I am not quite certain why you do not wish to use it. Given your existing strings.xml from above, have an arrays.xml with:

<string-array name="options">
<string>@string/option_resume</string>
<string>@string/option_analisys</string>
<string>@string/option_categories</string>
<string>@string/option_settings</string>
</string-array>

Then, requesting the options string array at runtime will give you the desired strings.

If your concern is manually maintaining that arrays.xml file, write yourself a bit of code to generate it as part of your build (e.g., custom Gradle task, custom Gradle plugin).

The slow and sloppy way of doing this would be to use Java reflection to iterate over all R.string fields for your app's R class, find their names, see which ones match, then use getString() to get the corresponding string values.

How to call resources with string name in android?

We can list all the names of raw assets, which are basically the filenames without the extensions, we can do this as -

public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}

Next you'll need to refer to them by the integer assigned to that resource name. You can get this integer as:

int resourceID=fields[count].getInt(fields[count]);

This is the same int which you'd get by referring to R.raw.yourRawFileName.

In your code you can use this like -

hoho= MediaPlayer.create(getApplicationContext(), resourceID);
hoho.start();

Next you can apply JAVA logic to play previous current and next media files. Please let know if you need help here also.

How to get string from resources strings into a fragment

You can use:

getResources().getString(R.string.my_string);

or just:

getString(R.string.my_string);

R.string; get string from dynamic key name

As your resources cannot be dynamically, you could use a switch statement like:

String name = "";
switch (row.getNameKey()) {
case keyName1:
name = getString(R.string.keyName1);
break;
case keyName2:
name = ...
...
}

Another approach woould be the getIdentifier method: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

and see: Android: Accessing string.xml using variable name



Related Topics



Leave a reply



Submit