Wednesday, 2 March 2016

Xamarin Android get drawable by filename

I had to tap into a carousel event and change a background image when it was swiped. The contents of the carousel were populated by a list of objects, with the image file name as a string property. I had to get the drawable with its filename, and prevent the process (getting the file from disk) blocking the UI. Once obtained I had to update the image on the UI thread.

 ThreadPool.QueueUserWorkItem(o =>
            {
                var imgId = Resources.GetIdentifier(_tips[position].Image, "drawable", AppContext.PackageName);
                var drawable = Resources.GetDrawable(imgId);
                RunOnUiThread(() =>
                {
                    _bgImage.SetImageDrawable(drawable);
                });
            });

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!