相关文章推荐
傲视众生的鞭炮  ·  boost serialization ...·  17 小时前    · 
眼睛小的单车  ·  java string 包含\n ...·  14 小时前    · 
买醉的闹钟  ·  Help to filter in ...·  1小时前    · 
果断的石榴  ·  QueryableExtensions.To ...·  1 月前    · 
非常酷的可乐  ·  getParcelableArrayList ...·  8 月前    · 
卖萌的充电器  ·  Python ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm attempting to get the int resource id for a layout resource by name, using Resources.GetIdentifier() of the Android API, but it returns 0. I'm using c#/monodroid/Xamarin, but regular java Android knowledge would apply too I suspect. Here's my code:

int resId = Resources.GetIdentifier(typeName, "layout", _activity.PackageName);

Where typeName = "FrmMain", and in my project I have the file "Resources/Layout/FrmMain.axml". Any ideas?

This is old, but for everyone getting this problem, I think it is because the resource name should be in lower case, so:

int resId = Resources.GetIdentifier("FrmMain", "layout", _activity.PackageName);

does not work, but:

int resId = Resources.GetIdentifier("frmmain", nameof(Resource.Layout).ToLower(), _activity.PackageName);

should work

edit:
According to this answer, you can (and should) use reflection to achieve what you're after, so I think you would try something like this:

var resourceId = (int)typeof(Resource.Layout).GetField(typeName).GetValue(null);

which does seem to work on my app and should get what you're after.

No because I need to look up the resource dynamically; sorry, forgot to mention that. "typeName" could be one of many things. "FrmMain" is just what I'm debugging with currently. – voxoid Dec 3, 2013 at 16:02 Thanks. This solution does work, and it's the one I had used myself as a work around, but I still don't understand why Resources.GetIdentifier() was not finding the resource; it seems that function is the more "correct" way of getting a resource id dynamically, rather than reflecting on the generated code in Resources. Nevertheless it works. – voxoid Dec 9, 2013 at 14:33 The code with reflection worked. I was trying to user the Resources.GetIdentifier inside a Fragment. May be thats why it was not working there. – Aman Sura Jul 1, 2016 at 20:30

In my case, this issue came up when I had to upgrade the target SDK due to google's new policy since November, 2018.

I had to display some strings according to the server response code (ex : api_res_001_suc), but it did not work on the upgraded version.

The overall version, about 22 as I recall, had to be changed to 27.

The cause of the issue seems to be the default translation stuff. When I put all the default translation for every string, it worked.

My code is,

getResources().getIdentifier(resName, "string", "packageName");

I've created a ResourceHelper class to handle this situation. Here is the code:

public static class ResourceHelper
    public static int FindId(string resourceId)
        var type = typeof(Resource.Id);
        var field = type.GetField(resourceId);
        return (int)field.GetRawConstantValue();
    public static int FindLayout(string layoutName)
        var type = typeof(Resource.Layout);
        var field = type.GetField(layoutName);
        return (int)field.GetRawConstantValue();
    public static int FindMenu(string menuName)
        var type = typeof(Resource.Menu);
        var field = type.GetField(menuName);
        return (int)field.GetRawConstantValue();

Actually I'm improving it because I need to use it from another Assembly and it's restricted to work in the same Assembly of the Droid App. I'm thinking about put a generic method (or an Extension one) to do this. Here is a draft of my idea:

public static int FindResource<T>(string resourceName)
    var type = typeof(T);
    var field = type.GetField(resourceName);
    return (int)field.GetRawConstantValue();

Hope it can help you.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.