Xamarin - Android Resources


When a new Android project is created, there are some files that are added to the project, by default. We call these default project files and folders as Android Resources. Take a look at the following screenshot.

Android Resources

The default Android resources include the following −

  • AndroidManifest.xml file − It contains information about your Android applications, e.g., the application name, permissions, etc.

  • Resources folder − Resources can be images, layouts, strings, etc. that can be loaded via Android’s resource system.

  • Resources/drawable folder − It stores all the images that you are going to use in your application.

  • Resources/layout folder − It contains all the Android XML files (.axml) that Android uses to build user interfaces.

  • The Resources/values folder − It contains XML files to declare key-value pairs for strings (and other types) throughout an application. This is how localization for multiple languages is normally set up on Android.

  • Resources.designer.cs − This file is created automatically when the Android projected is created and it contains unique identifiers that reference the Android resources.

  • MainActivity.cs file − This is the first activity of your Android application and from where the main application actions are launched from.

Resource files can be accessed programmatically through a unique ID which is stored in the resources.designer.cs file. The ID is contained under a class called Resource. Any resource added to the project is automatically generated inside the resource class.

The following code shows how to create a gridview project containing seven images −

namespace HelloGridView { 
      ("Xamarin.Android.Build.Tas ks", 
   public partial class Resource { 
      static Resource() {     
      public static void UpdateIdValues() {} 
      public partial class Attribute { 
         static Attribute() { 
         private Attribute() {} 
      public partial class Drawable { 
         // aapt resource value: 0x7f020000 
         public const int Icon = 2130837504; 
         // aapt resource value: 0x7f020001 
         public const int img1 = 2130837505; 
         // aapt resource value: 0x7f020002 
         public const int img2 = 2130837506;
         // aapt resource value: 0x7f020003 
         public const int img3 = 2130837507; 
         // aapt resource value: 0x7f020004 
         public const int img4 = 2130837508; 
         // aapt resource value: 0x7f020005 
         public const int img5 = 2130837509; 
         // aapt resource value: 0x7f020006 
         public const int img6 = 2130837510; 
         // aapt resource value: 0x7f020007 
         public const int img7 = 2130837511; 
         static Drawable() { 
         private Drawable() {} 
      public partial class Id { 
         // aapt resource value: 0x7f050000 
         public const int gridview = 2131034112; 
         static Id() { 
         private Id() {} 
      public partial class Layout { 
         // aapt resource value: 0x7f030000 
         public const int Main = 2130903040;
         static Layout() { 
         private Layout() {} 
      public partial class String { 
         // aapt resource value: 0x7f040001 
         public const int ApplicationName = 2130968577; 
         // aapt resource value: 0x7f040000 
         public const int Hello = 2130968576; 
         static String() { 
         private String() {} 

From the above code, the seven images are referenced in a class called drawable. These images are added programmatically. If a user adds another image to the project, it will also be added to the drawable class. The gridview contained in the project is also added and stored in a class on its own. Each item contained in the resources folder is automatically generated and stored in a class.