Introduction

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.

Look at following screenshot to understand what is Resources in Xamarin Android,

Xamarin
Resources are very important part of Xamarin android

So, basically what is Resources?

Resources are non-source code files and compiled (along with the source code) during the build process and packaged as an APK for distribution and installation onto devices.

Advantages of Resources are,

  1. Compile time checking.
  2. Target multiple devices.
  3. Code separation.

So, we are trying to understand resources in Xamarin Android in the following ways:

  1. Load Images in a layout and programmatically.
  2. Differences between Assets and Resources
  3. Images for different screen densities.

For this we are creating a sample project as below,

Load Images in a layout and programmatically.

Now Load Image by Programmatically

Difference between Assets and Resources

Sr. NoResourcesAssets
1. It Support is for different languages, OS versions and screen orientationNone of this available for Assets
2.It checked at compile time so we don't make a mistakeNot applied in Assets
1. Resources defined in a library project are automatically imported to application projects that depend on the libraryFor Assets, that don't happen; asset files must be present in the assets directory of the application project
2. Resource directory each file is given a pre-compiled ID which can be accessed easily through R.id. [res id]. This is useful to quickly and easily access images, sounds, icons.Asset directory is more like a filesystem and provides more freedom to put any file you would like in there.
3. Contains application resources, such as drawable files, layout files, and string values.This is empty. You can use it to store raw asset files. Files that you save here are compiled into an. apk file as-is, and the original filename is preserved.

Images for different screen densities.

Now we are trying to understand the difference between different densities of images. Android itself runs on many different devices, each having a wide variety of resolutions, screen sizes, and screen densities.

Screen Density

The number of pixels in any given area on the screen. The typical unit of measure is dots per inch (dpi).

Conclusion

Hence we learned what Resources in Xamarin Android are, advantages of Resources, how to load images by using designer as well as programmatically, the difference between Assets and Resources, and images for different screen densities.