Xamarin – walk-through – (Part 2)

Sample application created on android:
See full sample at: https://developer.xamarin.com/guides/android/getting_started/hello,android/hello,android_quickstart/

This is how layout looks like

string resources are located under Strings.xml

PhoneTranslator.cs file will be used by MainActivity.cs

[Activity (Label = “Phone Word 2”, MainLauncher = true, Icon = “@mipmap/icon”)] section defines what will be the title of the view.

making a call from button click

AndroidManifest.xml is the file for all meta settings for android application

Assets Contains the files the application needs to run including fonts, local data files, and text files. Files included here are accessible through the generated Assets class.

The Properties folder also houses AssemblyInfo.cs, a .NET assembly metadata file. It is good practice to fill this file with some basic information about your application.

Resources Contains application resources such as strings, images, and layouts. We can access these resources in code through the generated Resource class.

drawable, layout, values and Resource.designer.cs

Android applications don’t have a single entry point – there’s no single line of code in the application that the operating system calls to start the application. Instead, an application starts when Android creates one of its classes, during which Android loads the entire application’s process into memory.

An Activity is a special Android class that corresponds to a single application screen, and is responsible for drawing and powering the user interface

The Activity class contains the code that powers the user interface. The Activity is responsible for responding to user interaction and creating a dynamic user experience.

The Activity Attribute registers the Activity with the Android Manifest, letting Android know that this class is part of the Phoneword application managed by this Manifest.

The Label property sets the text to display at the top of the screen, and the Icon sets the image to display next to the text.

To look up a control, we call FindViewById and pass in the resource ID of the control:

You can use Intents to launch other Activities (screens) and even other applications.

The emulator is a good start for deploying and testing an application, but users will not consume the final app in the emulator. We should test applications on a real device early and often.

Android provides different versions of the drawable folder to handle images of different densities. Android will pick the icon with the appropriate density.

Great resources for assets and designs for Android Apps:

Please check this link for sample Xamarin Application: https://github.com/reyou/Sample-Xamarin-Phoneword

Intents are similar to asp.net view-bags. They can carry extra meta data information with them into new Activity that will be launched.

below, phoneNumbers collection is getting filled once call button is pressed.

The two most important Application Blocks are Activities and Services:

  • Activity – An Activity corresponds to a screen with a user interface, and it is conceptually similar to a web page in a web application. For example, in a news feed application, the login screen would be the first Activity, the scrollable list of news items would be another Activity, and the details page for each item would be a third.
  • Service – Android Services support Activities by taking over long-running tasks and running them in the background. Services don’t have a user interface and are used to handle tasks that aren’t tied to screens – for example, playing a song in the background or uploading photos to a server.

Android Navigation

Launching Another Application with an Intent

Understanding Android API Levels

Xamarin.Android exposes three Android API level project settings:

  • Target Framework – Specifies which framework to use in building your application. This API level is used at compile time by Xamarin.Android.
  • Minimum Android Version – Specifies the oldest Android version that you want your app to support. This API level is used at run time by Android.
  • Target Android Version – Specifies the version of Android that your app is intended to run on. This API level is used at run time by Android.

As the Android platform evolves and new Android versions are released, each Android version is assigned a unique integer identifier, called the API Level.

The target framework and the target Android version, while having very similar names, are not the same thing. The target framework setting communicates target API level information to Xamarin.Android for use at compile time, while the target Android version communicates target API level information to Android for use at run time (when the app is installed and running on a device).

Target Framework => compile time.
Android Version => run time.

For each API that is not available on Android platforms corresponding to your minimum Android version setting, your code must explicitly check the value of the Android.OS.Build.Version.SdkInt property to determine the API level of the platform the app is running on. If the API level is lower than the minimum Android version that supports the API you want to call, then your code has to find a way to function properly without making this API call.

There’s no fast and simple rule that explains how to reduce or modify your app’s functionality when it runs on older Android versions that are lacking one or more APIs.

The following best practices should be observed when consuming or creating libraries:

  • When consuming an Android library – If you are consuming an Android library in your application, be sure to set your app’s target framework setting to an API level that is at least as high as the target framework setting of the library.
  • When creating an Android library – If you are creating an Android library for use by other applications, be sure to set its target framework setting to the minimum API level that it needs in order to compile.

Removing unused resources

There are two ways to access resources in a Xamarin.Android application: programmatically in code and declaratively in XML using a special XML syntax.

Alternate resources are specified by adding a short string, called a qualifier, to the end of the directory holding a given type of resources.

For example, resources/drawable-de will specify the images for devices that are set to a German locale, while resources/drawable-fr would hold images for devices set to a French locale.

The five files that make up the default resources were created in the Resources folder:

Icon.png – The default icon for the application

Main.axml – The default user interface layout file for an application. Note that while Android uses the .xml file extension, Xamarin.Android uses the .axml file extension.

Strings.xml – A string table to help with localization of the application.

Resource.designer.cs – This file is automatically generated and maintained by Xamarin.Android and holds the unique ID’s assigned to each resource. This is very similar and identical in purpose to the R.java file that an Android application written in Java would have. It is automatically created by the Xamarin.Android tools and will be regenerated from time to time.

AboutResources.txtThis is not necessary and may safely be deleted. It just provides a high level overview of the Resources folder and the files in it.

If for some reason the Build Action is not set to Android Resource, then the files will be excluded from the APK, and any attempt to load or access the resources will result in a run-time error and the application will crash.

Android only supports lowercase file names for resource items, Xamarin.Android will support both uppercase and lowercase file names.

The convention for image names is to use lowercase with underscores as separators (for example, my_image_name.png). Resource names cannot be processed if dashes or spaces are used as separators.

Resources in an XML file are accessed by a following a special syntax:

A complete list of resource types: animator, anim, color, drawable, layout, menu, raw, values, xml.

Alternate resources are organized as a sub-directory inside the Resources folder according to the resource type, just like default resources. The name of the alternate resource subdirectory is in the form <ResourceType>-<Qualifier>.

Android determines the resources base by iterating over the following test of rules:

If Android cannot find a resource set that matches a given screen size, the application will crash.

When developing applications, resolution is not as important as screen size and density.

Screen Resolutions, Pixel Density and Aspect Ratio

Understanding / What are Pixels & Resolutions

Density-independent pixel (dp) – This is a virtual unit of measure to allow layouts to be designed independent of density. To convert dp into screen pixels the following formula is used: px=dp*dpi/160

Density-independent Pixels

Leave a Reply

Your email address will not be published. Required fields are marked *