Shared/External/Reusable Resources in an External Library

Nathan Sokalski 4,111 Reputation points
2021-03-31T15:44:30.973+00:00

I, like many developers, I have many android resources (drawables, styles, layouts, etc.) that I want to use in multiple apps. Is there any way to place these in a Class Library (or some other project type) so that they can be referenced & used by multiple Xamarin.Android projects? I have been unable to find a way to do this. It seems very inefficient to need to copy/update the file(s) in every project every time, not to mention it breaks one of the #1 goals of OOP: Code Reusability. Is there a way to share resources between projects? Thanks.

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

Accepted answer
  1. Nathan Sokalski 4,111 Reputation points
    2021-04-08T15:31:19.683+00:00

    I found something (I forget where) that suggested using the actual app template and removing the AndroidApplication tag from the *.csproj file (as well as removing several files like MainActivity.cs, activity_main.xml, and the mipmap directories). This has worked so far, so I will just stick with that until Visual Studio 2019 has an appropriate template. Thanks for your help!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,941 Reputation points Microsoft Vendor
    2021-04-01T06:29:37.373+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can Create the Android Class Library to achieve it. But I find you just could create normal class library or F#'s Android Class Library.

    Create a layout folder (or drawable, styles) in this class library and create a layout in this folder.

    83541-image.png

    Then you can use it in your xamarin.android project. I test it with Drawables and Layouts,

       protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
         
                   var btn_Load = FindViewById<Button>(Resource.Id.btn_LoadLibraryLayout);  
         
                   var imagev = FindViewById<ImageView>(Resource.Id.imageView1);  
                    
                   imagev.SetBackgroundDrawable(ApplicationContext.GetDrawable(ClassLibrary1.Resource.Drawable.my_icon));  
         
                   btn_Load.Click += delegate  
                   {  
                       SetContentView(ClassLibrary1.Resource.Layout.library_layout);  
                   };  
         
         
               }  
    

    Here is a similar thread:

    https://stackoverflow.com/questions/58459868/xamarin-android-layout-resources-in-library-project-not-included-in-application

    83497-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.