How i can return id of resource, if i know name of resource?
Something like this:
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
I'll use getResources(). getIdentifier(name, "id", getPackageName()); to get the ID of an ImageButton (as you would with R.id.name). @Srujan Barai, getResource() and getPackageName() are methods from Activity.
Description The Resource ID field contains the identifier number that Project assigns to each resource. The Resource ID indicates the position of the resource in relation to other resources. How Calculated As you add resources, Project automatically assigns the next number in the sequence of resources as listed.
int resourceId = this. getResources(). getIdentifier("nameOfResource", "id", this.
In MonoDroid, that code translates to something like:
var resourceId = Resources.GetIdentifier(mDrawableName.ToLower(), "drawable", PackageName);
However, I personally don't use GetIdentifier as it's not recommended in Android - so I use Reflection instead:
var resourceId = (int)typeof(Resource.Drawable).GetField(mDrawable).GetValue(null);
although that code should definitely have some try/catch error checking added for arbitrary strings!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With