1

I'm following this tutorial and work fine. https://developer.xamarin.com/recipes/android/controls/imageview/display_an_image

But in my case, I have 200 pictures. The user will write word in EditText, after click in button, show picture respective. How can I change picture source in code below:

 EditText edit = FindViewById<EditText>(Resource.Id.edtName);

   button.Click += delegate 
        {
            img.SetImageResource(Resource.Drawable.sample2);

        }; 

where "sample2" change for edit.Text. (text write for user...)

Diego VenĂ¢ncio
  • 5,698
  • 2
  • 49
  • 68

1 Answers1

1

Use a method to change the drawable

    public void changePhoto()
    {
        int MyPhoto;
        if (edit.Text != string.Empty)
        {
            try
            {
                MyPhoto = (int)typeof(Resource.Drawable).GetField(edit.Text).GetValue(null);
            }
            catch
            {
                MyPhoto = Resource.Drawable.ErrorPhoto;
            }
            img.SetImageResource(MyPhoto);
        }
    }
    button.Click += delegate 
    {
        changePhoto();
    }; 
Mohamed Krayem
  • 418
  • 8
  • 19