image files in Resources drawable do not appear in Xamrin.Forms Android

Jeff Whattam 101 Reputation points
2021-05-19T15:38:33.033+00:00

I have a custom map renderer where I want to add custom pin. I have the pin's image file added in Resources/drawable. But when I specifically used Android code to locate the file, I found that it didn't exist. The method I called is something like this:

BitmapDescriptorFactory.FromResource(Resource.Drawable.CustomPin);

I know that in native Android, the images will have an integer identification, so I think that CustomPin does not have its integer identification generated and that is why it cannot be found. If this is the case, what can I do to have the image files I put in Resources/drawable to have their integer ids generated?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jeff Whattam 101 Reputation points
    2021-06-02T14:13:43.53+00:00

    @Leon Lu (Shanghai Wicresoft Co,.Ltd.) I finally figured it out. Somehow I have to use the full path of the icon file. For example, if my project name is MyApp, I have to use MyApp.Droid.Resource.Drawable.Pin. Not sure why in the sample project, this is not needed and can be access as Resource.Drawable.Pin.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,486 Reputation points Microsoft Vendor
    2021-05-20T05:54:38.883+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Which error did you meet? Or you do not get any error, but image do not appear after setting it to pin of custom renderer map.

    If you meet error: 'Resource.Drawable' does not contain a definition for 'icon1' and you have added the image to Resources/drawable folder. Please open your project folder delete bin and obj folder, then rebuild your project.

    If you set the image to MarkerOptions's icon in custom renderer for map like following code, but these pins do not disappear.

       protected override MarkerOptions CreateMarker(Pin pin)  
               {  
                   var marker = new MarkerOptions();  
                   marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));  
                   marker.SetTitle(pin.Label);  
                   marker.SetSnippet(pin.Address);  
                   marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.icon1));  
                   return marker;  
               }  
    

    You can refer to following samples about custom renderer for map. I set a new icon in custom renderer. it works like following screenshot.
    https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/customrenderers-map-pin/

    98161-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.