How to access resource with dynamic name in my case?
int id = getResources().getIdentifier(imageName, type, package);
This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.
Using only the name
parameter:
You can also include all the 3 info in the "name" parameter using the following format: "package:type/image_name"
, something like:
int id = getResources().getIdentifier("com.my.app:drawable/my_image", null, null);
This is useful when you're working with external components or libraries that you can't, or don't want to, change how getIdentifier()
is called. e.g.: AOSP Launcher3
Read a string stored in a resource file (resx) with dynamic file name
Will something like this help in your case?
Dictionary<string, string> resourceMap = new Dictionary<string, string>();
public static void Func(string fileName)
{
ResXResourceReader rsxr = new ResXResourceReader(fileName);
foreach (DictionaryEntry d in rsxr)
{
resourceMap.Add(d.Key.ToString(),d.Value.ToString());
}
rsxr.Close();
}
public string GetResource(string resourceId)
{
return resourceMap[resourceId];
}
How to access the values from strings.xml dynamically?
Use the method getIdentifier(name, defType, defPackage)
of the Resources class to get the id of a resource by name. Then you can do a normal getString(id)
from the same class.
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
Dynamic reference to resource files in C#
You'll need to instance a ResourceManager
for the Login.resx
:
var resman = new System.Resources.ResourceManager(
"RootNamespace.Login",
System.Reflection.Assembly.GetExecutingAssembly()
)
var text = resman.GetString("resname");
It might help to look at the generated code in the code-behind files of the resource files that are created by the IDE. These files basically contain readonly properties for each resource that makes a query to an internal resource manager.
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
DataBinding: How to get resource by dynamic id?
I ended up creating my own method:
public class BindingUtils {
public static String string(int resourceId) {
return MyApplication
.getApplication()
.getResources()
.getString(resourceId);
}
}
Declaring an import for it:
<data>
<import type="com.example.BindingUtils" />
...
</data>
And just calling it during binding:
android:text="@{@string/myFormatString(BindingUtils.string(myPojo.resourceId))}"
Would be nice to have out-of-the-box method for that. DataBinding is sitll in Beta - so maybe it will come in future.
Getting a string dynamically from strings resources
A little searching did the trick. I have the right ResourceManager
available in my strings
class:
ResourceManager rm = strings.ResourceManager;
string someString = rm.GetString("someString");
Related Topics
Package Signatures Do Not Match The Previously Installed Version
Google Map for Android My Location Custom Button
How to Put My Libraries in Front of Android.Jar by Editing Build.Gradle in Android-Studio
How to Access The Second Mic Android Such as Galaxy 3
Findbugs Idea - Classnotfoundexception Com.Google.Wireless.Android.Sdk.Stats.Intellijindexingstats
How to Get a Button's Height to Match Another Element's Height
Android Listview with Checkbox Problem
Switch Back/Front Camera on Fly
How to Enable Night Mode Programmatically
How to Generate The Aac Adts Elementary Stream with Android Mediacodec
Android: Execute Code in Regular Intervals
Android, How Do How to Get a List of All Files in a Folder
Android Gcm Basic Implementation