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!
Shared/External/Reusable Resources in an External Library
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.
1 additional answer
Sort by: Most helpful
-
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.
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:
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.