Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the ImageView source dynamically from a string? (Xamarin Android)

I'm using Xamarin Android and I'm trying to change the ImageView src dynamically. All the pictures are placed in Resource/drawable. After reading the picture name as string from an xml (string imgName = nameFromXml + ".jpg") I would like to passing it to my ImageView. I guess there are two good ways:

1) Get the image (int)id and passing it to the ImageView using SetImageResource(): myTextView.SetImageResource(????????);

2) Get the Uri of the of the Image

EDIT
Unfortunately I couldn't use Eric answer for QuinDa question because there are many methods not existing on Xamarin Android, working just in Android.

like image 775
Masciuniria Avatar asked Dec 03 '22 14:12

Masciuniria


1 Answers

Here is the standard Android method:

int resImage = getResources().getIdentifier(imgName , "drawable", getPackageName());
myTextView.setImageResource(resImage);

Please note that imgName variable must not contain file extension.

Xamarin.Android C#:

int resImage = Resources.GetIdentifier(imgName, "drawable", PackageName);
like image 111
Romuald Leroux Avatar answered Dec 06 '22 12:12

Romuald Leroux