question

JeffWhattam-6305 avatar image
0 Votes"
JeffWhattam-6305 asked JeffWhattam-6305 answered

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

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?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JeffWhattam-6305 avatar image
0 Votes"
JeffWhattam-6305 answered

@LeonLu-MSFT 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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://docs.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.



image.png (213.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT Thank you for the reply! The first error you described matches the one I had. I followed your instructions by deleting bin and obj folders and rebuilding the project. But the image file is still not identifiable. The link you attached is exactly the one I used for learning to create customized pin. But I still have no idea how the pin image file cannot be recognized in my project.

0 Votes 0 ·

First of all, you can run this application directly, just ignore this error, if you can run normally in the emulator or android device. If you do not run it in emulator, please repair your VS.

98413-image.png


0 Votes 0 ·
image.png (52.8 KiB)

This demo have an icon in the drawable folder, you can set this icon to make a test.

0 Votes 0 ·