Xamarin.Android Application FundamentalsHere, we will explain some tasks or concepts with which the developer needs to be aware of during the development of the Android applications. AccessibilityHere, we will discuss how to use the Android Accessibility APIs to build the apps. UI Elements Android provides a ContentDescription property that is used by the screen reading APIs to describe the purpose of control's accessibility. The description of the content is in the AXML layout file. AXML layout In XML layout, we use the android:contentDescription attribute: Hint for TextView For the data input, EditText and TextView controls use the Hint property to provide the description of the expected input. When the text is entered, text itself will "read" instead of hint. AXML layout XML layout files uses the android:hint attribute: LabelFor links input fields with labels To associate label with a data input control, we use the labelFor property. AXML layout In XML layout we use the android:labelFor property to reference the another control's identifier: Announce for Accessibility We use the AnnounceForAccessibility method on any view control to communicate an event or status change to users when accessibility is enabled. AnnounceForAccessibility is not required for all the operations where the built-in narration provides sufficient feedback. We can use AnnounceForAccessibility where additional information will be helpful for the user. The code below shows a simple example calling AnnounceForAccessibility: Changing Focus Settings Navigation depends on controls having focus on the user's understanding of the availability of the operation. Android provides a Focusable property that can have the tag controls specifically able to receive the focus during navigation. AXML layout In layout XML files, set the Android: focusable attribute: We can focus on the order of the control with the nextFocusDown, nextFocusLeft, nextFocusup attributes, which is set in the XAML layout. We use these attributes to ensure that the user can navigate easily through the controls on the screen. Android ResourcesHere, we will show the concept of Android Resources in Xamarin. An Android application is just a source code. Many files can make the application such as video, images, fonts, and audio files. The non-source code file referred to as resources and compiled during the build process and packaged as an APK for the distribution and installation into the devices: Resources offer different advantages to an Android application:
When a new Xamarin.Android project is started, an exclusive directory Resources is created, along with some subdirectories: This screenshot shows that the application resources are arranged according to their type into the subdirectories. Drawable Directory has the Images; views go in the layout subdirectory, etc. Here, are the two ways to access the Resources in Xamarin. Android application: Programmatically in code and declaratively in XML using XML syntax. These Resources are called the Default Resources and used by all the devices unless a more specific match is found. Each type of resource may have Alternate Resources that Android may use to target particular tools. For example, resources provided to target the locale and screen size of a user. If the device rotates in 90 degrees from portrait to landscape, then in all the cases, Android will load the resources for the application without any extra coding effort. At the end of the directory which holds other resources are specified by adding a short string is known as a qualifier. For example, resources/drawable-de will specify the images for devices that are set to a German locale, while resources/drawable-fr hold the images for devices set to a French region. The example of providing other resources can be seen in the image below where the same application is being run with changing the locale of the device: Here, we will discuss some more basics terms related to Resources. These are: Android Resource BasicsAlmost all the Android application has some Resources. Applications often have the user interface layout in the form of XML files. When we create the Xamarin.Android application first, default resources are set up by the Xamarin.Android project template. As shown in the screenshot, here are the five files which created the default Resources in Resource Folder:
Creating and Accessing the ResourceCreating the Resources is as simple as adding the files to the directory for the Resources. When Strings.xml added to the file, Build Action was automatically set to AndroidResources by the Xamarin.Android tools: This allows the Xamarin. Android tools to compile properly and embed the resources into the APK file. If, for any reason, Build Action not set to Android Resources, then the data will be excluded from APK. At the time of loading or access, the Resources give the Run-Time error, and the application crashes. Android supports only lowercase filenames for resource items while Xamarin. Android supports both uppercase and lowercase. The convention for image names is to use the lowercase with underscores as a separator, for example, my_image_name.png. The name of the Resources cannot be processed if dashes or spaces are used as separators. Referencing the Resources ProgrammaticallyTo access all the Resource files programmatically, Unique ResourceID is assigned to the data. The resource ID is an integer defined in a particular class called Resources, which is found in the file Resource.Designer.CS. Code looks something like this: Each ResourceID is found inside the nested class, which corresponds to the type of Resources. When the icon.png file was added to the project, Xamarin.Android updated the Resource class and created a nested class known as Drawable with a constant name icon. Drawable allows the file icon.png to be referred to in the code as Resource.Drawable.Icon. Resource class should not be edited manually; the Xamarin will overwrite all changes in the Android. When we reference the Resource programmatically, they can be accessed by the Resource Class hierarchy. Here we use the following syntax: PackageName: Package, which provided the Resource that required the resources from another package where it is used. Resource Type: This is the type of nested Resource within the Resource class. Resource Name: This is the filename of Resource (without file type extension) or the value of the Android: name attribute for resources that are in the XML element. For example, the content of the layout file, main. axml are: In this example, we used the ImageView, which requires a drawable resource name flag. ImageViewhas an src attribute which is set to @drawable/flag. When an activity starts, Android will look inside the directory Resource/Drawable for a file flag.png (the file extension could be another image format, like flag.jpg) and load the file and display it in the ImageView. When this application run, it would look something like the following image: Default ResourcesDefault Resources are the items that are not specific for any particular device. If no specific resource found, then it is the default choice for the Android OS. Default Resources is the most common Resource. They are arranged into the sub-directories of the Resource directory according to the type of Resources. From the above image, it shows the default values for the drawable resources, layouts, and values (XML file, which contains simple value). The list of the type of Resources are:
Alternate ResourceAlternate Resources are those resources that target the specific device. Alternative Resource is also defined as a run-time configuration such as the current language, screen size, and the pixel density. If android can match the Resource that is specific to particular devices or configuration than the default resource, in that case, we will use Alternate Resources. If Android didn't find any other resources that match the current configuration, then we will load default resources. The name of the Alternate Resource Sub-Directory is Resource-type-qualifier. The qualifier is a name that identifies the configuration of the specific device. A name can contain more than one qualifier; dash separates each of them. From the above screenshot, it shows that this contains the other resources for various configurations such as locale, screen size, screen density, and orientation. The rules apply while adding the qualifiers to the Resources are:
List of Qualifiers are:
Android Determine what Resources to useThe android application contains many resources. Here we will study how Android will select the Resources for the application when it runs on a device. Android shows the base of the Resources by iterating over the rules of the Test:
When the system is looking for resources and cannot find them, it will find other resources and scale them. Android cannot find the default resources. For example, when Android is looking for a low-density resource, and it is not available, Android may select the version of high-density Resource over the default or medium-density resources. It does this because, the high-density Resource can be measured by a factor of 0.5, which will result in a few visibility issues than measured a medium-density resource, which would require a factor of 0.75. For example: Consider an application which has the following drawable resource directories: And then the application runs on a device with the following configuration: French resources are eliminated conflict with the locale of en-GB: The first qualifier is selected from the qualifiers table MCC and MNC. There are no resource directories which contain this qualifier, so the MCC/MNC code is ignored. The next qualifier selects the language. Some resources match the language code. All resource directories which do not match with the language code are rejected and list of resources are now: The next qualifier present for screen orientation is the resource directories that do not match with the screen orientation port are eliminated: The qualifier for screen density is ldpi, which results in the exclusion of one more resource directory: As a result, Android will use the drawable resources in the resource directory drawable-en-port-ldpi for the device. Next TopicXamarin vs Ionic |