Xamarin Android PDF
Xamarin Android PDF
Android
#xamarin.an
droid
Table of Contents
About 1
Remarks 2
Versions 2
Examples 3
Introduction 8
Remarks 8
Examples 8
Application lifecycle 8
Activity lifecycle 9
Fragment lifecycle 11
Introduction 15
Examples 15
Sample Code 15
Chapter 4: Bindings 16
Examples 16
Removing Types 16
Examples 18
Custom Listview comprises of rows that are designed as per the users needs. 18
Chapter 6: Dialogs 24
Remarks 24
Examples 24
Alert dialog 24
Chapter 7: Dialogs 26
Parameters 26
Remarks 26
Examples 27
AlertDialog 27
Chapter 8: How to correct the orientation of a picture captured from Android device 30
Remarks 30
Examples 30
Introduction 38
Examples 38
Important 41
Understanding Xamarin.Linker 54
Understanding ProGuard 56
Examples 60
RecyclerView Basics 60
Examples 67
Introduction 69
Parameters 69
Examples 69
Send and receive data from and to bluetooth device using socket 69
Remarks 71
Examples 71
Credits 75
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: xamarin-android
It is an unofficial and free Xamarin.Android ebook created for educational purposes. All the content
is extracted from Stack Overflow Documentation, which is written by many hardworking individuals
at Stack Overflow. It is neither affiliated with Stack Overflow nor official Xamarin.Android.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with
Xamarin.Android
Remarks
Xamarin.Android allows you to create native Android applications using the same UI controls as
you would in Java, except with the flexibility and elegance of a modern language (C#), the power
of the .NET Base Class Library (BCL), and two first-class IDEs - Xamarin Studio and Visual Studio
- at their fingertips.
For more information on installing Xamarin.Android on your Mac or Windows machine, refer to the
Getting Started guides on the Xamarin developer center
Versions
https://riptutorial.com/ 2
Examples
Get started in Xamarin Studio
1. Browse to File > New > Solution to bring you up the new project dialog.
2. Select Android App and press Next.
3. Configure your app by setting your app name and organization ID. Select the Target
Platform most suited for your needs, or leave it as the default. Press Next:
https://riptutorial.com/ 3
https://riptutorial.com/ 4
4. Set your Project name and Solution name, or leave as the default name. Click Create to
create your project.
5. Set up your device for deployment, or configure an emulator
6. To run your application, select the Debug configuration, and press the Play button:
1. Browse to File > New > Project to bring you up the New Project dialog.
2. Navigate to Visual C# > Android and select Blank App:
https://riptutorial.com/ 5
https://riptutorial.com/ 6
3. Give your app a Name and press OK to create your project.
4. Set up your device for deployment, or configure an emulator
5. To run your application, select the Debug configuration, and press the Start button:
https://riptutorial.com/ 7
Chapter 2: App lifecycle - Xamarin.Andorid
Introduction
Xamarin.Android application lifecycle is the same as normal Android app. When talking about
lifecycle we need to talk about: Application lifecycle, Activity lifecycle and Fragment lifecycle.
In the below I'll try to provide a good description and way of using them. I obtained this
documentation from the official Android and Xamarin documentation and many helpful web
resources provided in remarks section below.
Remarks
Interesting links to broad your knowledge about Android application lifecycle:
https://developer.android.com/reference/android/app/Activity.html
http://www.vogella.com/tutorials/AndroidLifeCycle/article.html
https://github.com/xxv/android-lifecycle
https://developer.android.com/guide/components/fragments.html
https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-
_creating_a_fragment/
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Examples
Application lifecycle
First of all you should know that you can extend Android.Application class so you can access two
important methods related with app lifecycle:
• OnCreate - Called when the application is starting, before any other application objects have
been created (like MainActivity).
• OnTerminate - This method is for use in emulated process environments. It will never be
called on a production Android device, where processes are removed by simply killing them;
No user code (including this callback) is executed when doing so. From the documentation:
https://developer.android.com/reference/android/app/Application.html#onTerminate()
In Xamarin.Android application you can extend Application class in the way presented below. Add
new class called "MyApplication.cs" to your project:
https://riptutorial.com/ 8
[Application]
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle,
ownerShip)
{
}
As you wrote above you can use OnCreate method. You can for instance initialize local database
here or setup some additional configuration.
Activity lifecycle
Activity lifecycle is quite more complex. As you know Activity is single page in the Android app
where user can perform interaction with it.
On the diagram below you can see how Android Activity lifecycle looks like:
https://riptutorial.com/ 9
As you can see there is specific flow of Activity lifecycle. In the mobile application you have of
course methods in each Activity class that handle specific lifecycle fragment:
https://riptutorial.com/ 10
base.OnStart();
}
• The entire lifetime of an activity happens between the first call to onCreate(Bundle) through
to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(),
and release all remaining resources in onDestroy(). For example, if it has a thread running in
the background to download data from the network, it may create that thread in onCreate()
and then stop the thread in onDestroy().
• The visible lifetime of an activity happens between a call to onStart() until a corresponding
call to onStop(). During this time the user can see the activity on-screen, though it may not
be in the foreground and interacting with the user. Between these two methods you can
maintain resources that are needed to show the activity to the user. For example, you can
register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and
unregister it in onStop() when the user no longer sees what you are displaying. The onStart()
and onStop() methods can be called multiple times, as the activity becomes visible and
hidden to the user.
Fragment lifecycle
https://riptutorial.com/ 11
As you know you can have one activity but different fragments embedded in it. That is why
fragment lifecycle is also important for developers.
On the diagram below you can see how Android fragment lifecycle looks like:
As described in the official Android documentation you should implement at least below three
https://riptutorial.com/ 12
methods:
• OnCreate - the system calls this when creating the fragment. Within your implementation,
you should initialize essential components of the fragment that you want to retain when the
fragment is paused or stopped, then resumed.
• OnCreateView - The system calls this when it's time for the fragment to draw its user
interface for the first time. To draw a UI for your fragment, you must return a View from this
method that is the root of your fragment's layout. You can return null if the fragment does not
provide a UI.
• OnPause - The system calls this method as the first indication that the user is leaving the
fragment (though it does not always mean the fragment is being destroyed). This is usually
where you should commit any changes that should be persisted beyond the current user
session (because the user might not come back).
base.OnPause();
}
}
Of course you can add additional methods here if you want to handle different states.
If you would like to get base project with methods described below you can download
Xamarin.Android application template from my GitHub. You can find examples for:
https://riptutorial.com/ 13
• Application lifecycle methods
• Activity lifecycle methods
• Fragment lifecycle methods
https://github.com/Daniel-
Krzyczkowski/XamarinAndroid/tree/master/AndroidLifecycle/LifecycleApp
https://riptutorial.com/ 14
Chapter 3: Barcode scanning using ZXing
library in Xamarin Applications
Introduction
Zxing library is well known for image processing. Zxing was based on java and .Net module is also
available and it can be used in xamarin applications. click here to check official documentation.
http://zxingnet.codeplex.com/
step2: In whichever activity we need to show barcode scanner, in that activity initialise
MobileBarcodeScanner.
Step3: Write below code when tapped on any view to start scanning.
Examples
Sample Code
https://riptutorial.com/ 15
Chapter 4: Bindings
Examples
Removing Types
It is possible to instruct the Xamarin.Android Bindings Generator to ignore a Java type and not
bind it. This is done by adding a remove-node XML element to the metadata.xml file:
If a java library contains interfaces that should be implemented by the user (e.g. click listeners like
View.IOnClickListener or callbacks), the implementing class has to inherit -- directly or indirectly --
from Java.Lang.Object or Java.Lang.Throwable. This is a common error, because the packaging
steps just print a warning that is overlooked easily:
Type 'MyListener ' implements Android.Runtime.IJavaObject but does not inherit from
Java.Lang.Object. It is not supported.
Wrong
Correct
class MyListener :
Java.Lang.Object, // this is the important part
View.IOnClickListener
{
public void OnClick(View v)
{
// ...
}
https://riptutorial.com/ 16
}
Not everything in a bindings library will have the same name in C# as it does in Java.
In C#, interface names start with "I", but Java has no such convention. When you import a Java
library, an interface named SomeInterface will become ISomeInterface.
Similarly, Java doesn't have properties like C# does. When a library is bound, Java getter and
setter methods might be refactored as properties. For example, the following Java code
may be refactored as
https://riptutorial.com/ 17
Chapter 5: Custom ListView
Examples
Custom Listview comprises of rows that are designed as per the users needs.
https://riptutorial.com/ 18
android:textSize="15dip"
android:layout_toRightOf="@id/Image" />
</RelativeLayout>
Then you can design your main.axml, which contains a textview for the header and a listview.
Next create your Data.cs class that will represent your row objects
public Data ()
{
Heading = "";
SubHeading = "";
ImageURI = "";
}
}
Next you need the DataAdapter.cs class, Adapters link your data with the underlying view
List<Data> items;
https://riptutorial.com/ 19
Activity context;
public DataAdapter(Activity context, List<Data> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override Data this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CustomRow, null);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading;
return imageBitmap;
}
The most important part is inside the GetView Function, this is where you link your object to your
custom row.
https://riptutorial.com/ 20
The GetImageBitmapFromUrl is not part of the dataadapter but I have put this over here for
simplicity.
ListView listView;
myList.Add (obj);
https://riptutorial.com/ 21
obj1.Heading = "Banana";
obj1.SubHeading = "Bananas are an excellent source of vitamin B6 ";
obj1.ImageURI =
"http://www.bbcgoodfood.com/sites/bbcgoodfood.com/files/glossary/banana-crop.jpg";
myList.Add(obj1);
myList.Add(obj2);
myList.Add(obj3);
myList.Add (obj4);
listView.Adapter = new DataAdapter(this,myList);
https://riptutorial.com/ 22
Read Custom ListView online: https://riptutorial.com/xamarin-android/topic/6406/custom-listview
https://riptutorial.com/ 23
Chapter 6: Dialogs
Remarks
Setting the Context of the dialog
When creating a Dialog from an Activiy we can use this as the context.
Button types
SetNeutralButton() can be used for a simple notification and confirmation that the notification is
read. SetPositiveButton() can be used for a confirmation for example: "Are you sure you want to
delete this item?" SetNegativeButton() is for dismissing the dialog and cancelling it's action.
If we want to make sure that the user can't dismiss the dialog with the back button we can call
SetCanceable(false). This only works for the back button.
Rotation
If the screen is rotated whilst a dialog is visible it will be dismissed and the ok and cancel actions
will not be called. You will need to handle this inside your activity and re-show the dialog after the
activity has been reloaded.
Examples
Alert dialog
https://riptutorial.com/ 24
// code here for handling the Neutral tap
});
builder.SetCancelable(false);
builder.Show();
https://riptutorial.com/ 25
Chapter 7: Dialogs
Parameters
Remarks
Requirements
Namespace: Android.App
https://riptutorial.com/ 26
Public Constructors
AlertDialog.Builder(Context) :-
Constructor using a context for this builder and the AlertDialog it creates.
AlertDialog.Builder(Context, Int32) :-
Constructor using a context and theme for this builder and the AlertDialog it creates.
Examples
AlertDialog
dialog.Show();
Now considering you have gone through the getting started guide from the documentation.
https://riptutorial.com/ 27
Your Main Activity must be looking like this:
Now What we shall do is, instead of adding one to the counter on button click, we shall ask user if
he wants to add or substract one in a simple Alert Dialog
And on Click of the Positive or the negative button we will take the action.
button.Click += delegate {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Specify Action");
alert.SetMessage("Do you want to add or substract?");
screenshot:
https://riptutorial.com/ 28
https://riptutorial.com/ 29
Chapter 8: How to correct the orientation of a
picture captured from Android device
Remarks
1. This app sample is available on my GitHub below:
https://github.com/Daniel-
Krzyczkowski/XamarinAndroid/tree/master/AndroidPictureOrientation/PictureOrientationApp
https://components.xamarin.com/view/xamarin.mobile
Examples
How to correct the orientation of a picture captured from Android device
This example shows how to take image and display it correctly on the Android device.
Firstly we have to create sample application with one button and one imageview. Once user clicks
on the button camera is launched and after user selects picture it will be displayed with the proper
orientation on the screen.
https://riptutorial.com/ 30
2. Now open activity code behind:
ImageView _takenPictureImageView;
Button _takePictureButton;
_takenPictureImageView = FindViewById<ImageView>(Resource.Id.TakenPictureImageView);
_takePictureButton = FindViewById<Button>(Resource.Id.TakePictureButton);
_takePictureButton.Click += delegate
{
takePicture();
};
}
3. In our application we will use Xamarin Mobile component available in the Components Store:
https://riptutorial.com/ 31
4. Once you add it to the project we can move on. Add below code which is responsible for
launching camera. This method should be invoked in the button click as you can see in the
above code:
void takePicture()
{
var picker = new MediaPicker(this);
DateTime now = DateTime.Now;
var intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions
{
Name = "picture_" + now.Day + "_" + now.Month + "_" + now.Year + ".jpg",
Directory = null
});
StartActivityForResult(intent, 1);
}
5. Once user takes picture we should display it in the proper orientation. To do it use below
method. It is responsible for retrieveing exif information from the taken image (including
orientation during the moment of taking picture) and than creating bitmap with the proper
orientation:
https://riptutorial.com/ 32
height_tmp /= 2;
scale++;
}
options.InSampleSize = scale;
options.InJustDecodeBounds = false;
Bitmap resizedBitmap = BitmapFactory.DecodeFile(filePath, options);
https://riptutorial.com/ 33
}
return resizedBitmap;
}
6. Above method should be invoked in the OnActivityResult method invoked after user takes
the picture:
if (requestCode == 1)
{
if (resultCode == Result.Ok)
{
data.GetMediaFileExtraAsync(this).ContinueWith(t =>
{
using (Bitmap bmp = loadAndResizeBitmap(t.Result.Path))
{
if (bmp != null)
_takenPictureImageView.SetImageBitmap(bmp);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
https://riptutorial.com/ 34
https://riptutorial.com/ 35
That's it. Now you will have all you taken picture displayed in correct orientation.
https://riptutorial.com/ 36
Read How to correct the orientation of a picture captured from Android device online:
https://riptutorial.com/xamarin-android/topic/6683/how-to-correct-the-orientation-of-a-picture-
captured-from-android-device
https://riptutorial.com/ 37
Chapter 9: Publishing your Xamarin.Android
APK
Introduction
This topic shows information on how to prepare your Xamarin.Android app for release mode and
how to optimize it.
Examples
Preparing your APK in the Visual Studio
You finished your app, tested on debug mode and it is working perfect. Now, you want to prepare
it to publish in the Google Play Store.
https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_applicatio
Android Manifest
First, in Visual Studio, right-click your Xamarin.Android project in the Solution Explorer and select
Properties. Then, go to the Android Manifest tab, to see this screen:
https://riptutorial.com/ 38
Unlike in Android Studio or Eclipse, you don't need the set the AndroidManifest.xml file by writing;
Xamarin and Visual Studio do that for you. Activities, BroadcastReceivers and Services are
inserted into Android Manifest by declaring specific attributes in their classes.
• Application name: This is the app name that will be visible for the user.
• Package name: This is the package name. It must be unique, meaning that it must not use
the same package name of other apps in the Google Play Store.
• Application Icon: This is the icon that will be visible to the user, equivalent to the
@drawable/ic_launcher used in Android Studio or Eclipse projects.
• Version number: The version number is used by Google Play for version control. When you
want to publish an APK for an updated version of your app, you must add 1 to this number
for each new upgrade.
• Version name: This is the version name that will be displayed to the user.
https://riptutorial.com/ 39
• Install location: This determines where your APK will be installed, in the device storage or
SD Card.
• Required permissions: Here you determine which permissions are necessary for your app.
Android Options
In the screen below, you can configure the compiler options. Using the right options here can
reduce a lot your APK size and also prevent errors.
https://riptutorial.com/ 40
has less than 65536 methods, see here).
• Enable Proguard: true. This enables the Proguard tool that obfuscates Java code in your
app. Note that it does not apply to .NET code; if you want to obfuscate .NET code, you must
use Dotfuscator. More information on Proguard for Xamarin.Android can be found here.
• Enable developer instrumentation (debugging and profiling): false for Release APK.
• Linking: SDK and User Assemblies. This will make the Xamarin Linker to remove all
unused classes from SDK and your code, reducing the APK size.
Important
Xamarin.Linker may sometimes remove classes that are not seemed to be used by your code,
especially if they are in the project's Core (PCL library). To avoid that, you can either set the
Linking to "Sdk Assemblies Only" or use the Preserve attribute in your classes, example:
PreserveAttribute.cs
namespace My_App_Core.Models
{
public sealed class PreserveAttribute : System.Attribute
{
public bool AllMembers;
public bool Conditional;
}
}
In a class:
using System;
namespace My_App_Core.Models
{
[Preserve(AllMembers = true)]
public class ServiceException : Exception
{
public int errorCode;
[Preserve(AllMembers = true)]
public ServiceException() { }
[Preserve(AllMembers = true)]
public ServiceException(int errorCode)
{
this.errorCode = errorCode;
}
}
}
After configuring everything, Rebuild the Project to make sure that it builds successfully.
https://riptutorial.com/ 41
You finished configuring your Android project for Release. The tutorial below shows how to
generate the APK in Visual Studio. A full tutorial from Xamarin documentation can be found here:
https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_applicatio
_signing_the_android_application_package/
To create the APK file, right-click the Xamarin.Android project in the Solution Explorer and select
Archive...
This will open the Archive manager and begin archiving the project, preparing to create the APK
file.
https://riptutorial.com/ 42
When it finishes archiving the project, click in Distribute... to proceed.
The Distribute screen will present you two options: Ad-hoc and Google Play. The first will create
an APK and save it in your computer. The second will directly publish the app in Google Play.
Choosing the first is recommended, so you can test the APK in other devices if you want.
In the following screen, an Android Key Store is needed to sign the APK. If you already have one,
you can use it by clicking in Import...; if you don't, you can create a new Android Key Store by
clicking in +.
https://riptutorial.com/ 43
Creating a new Android Key Store screen:
https://riptutorial.com/ 44
To create the APK, click in Save As. You may be prompted to type the Key Store password.
https://riptutorial.com/ 45
https://riptutorial.com/ 46
When it completes, you can click in Open Folder on the Archives screen to see your generated
APK file.
https://riptutorial.com/ 47
Enabling MultiDex in your Xamarin.Android APK
MultiDex is a library in the Android APK that allows the app to have more than 65,536 methods.
The Android APKs have Dalvik Executable files (.dex) that contain the generated bytecodes
compiled from your Java code. Each .dex file can contain up to 65,536 methods (2^16).
Android OS versions before Android 5.0 Lollipop (API 21) use the Dalvik runtime, which only
supports one .dex file per APK, limiting to 65,536 methods per APK. Starting from Android 5.0, the
Android OS use ART runtime, which can support more than one .dex file per APK, avoiding the
limit.
To surpass the 65k methods limit in Android versions below API 21, the developers must use the
MultiDex support library. The MultiDex creates extra classes.dex files (classes2.dex, classes3.dex,
...) referencing them in the classes.dex file. When the app starts loading, it uses an
MultiDexApplication class to load the extra .dex files.
If your Android app aims for a minimum SDK version above or equal to API 21 (Android 5.0
Lollipop) it is not necessary to use the MultiDex library, because the OS handles natively the extra
.dex files. However, if for compatibility reasons the developer wants to support older Android OS,
then he/she should use the MultiDex library.
https://riptutorial.com/ 48
Then, you must create a MultiDexApplication class in your app. In the project's root, create a new
class (in the Solution Explorer, right-click in the project, Add.. -> Class, or Shift+Alt+C). In the new
class file, copy the following code, replacing the namespace Sample with the name of your
Xamarin.Android project namespace.
using System;
using Android.App;
using Android.Runtime;
using Java.Interop;
namespace Sample
{
[Register("android/support/multidex/MultiDexApplication", DoNotGenerateAcw = true)]
public class MultiDexApplication : Application
{
internal static readonly JniPeerMembers _members =
new XAPeerMembers("android/support/multidex/MultiDexApplication", typeof
(MultiDexApplication));
https://riptutorial.com/ 49
[Register(".ctor", "()V", "", DoNotGenerateAcw = true)]
public MultiDexApplication()
: base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
if (Handle != IntPtr.Zero)
return;
try
{
if (GetType() != typeof (MultiDexApplication))
{
SetHandle(
JNIEnv.StartCreateInstance(GetType(), "()V"),
JniHandleOwnership.TransferLocalRef);
JNIEnv.FinishCreateInstance(Handle, "()V");
return;
}
if (id_ctor == IntPtr.Zero)
id_ctor = JNIEnv.GetMethodID(class_ref, "<init>", "()V");
SetHandle(
JNIEnv.StartCreateInstance(class_ref, id_ctor),
JniHandleOwnership.TransferLocalRef);
JNIEnv.FinishCreateInstance(Handle, class_ref, id_ctor);
}
finally
{
}
}
If you are developing in Visual Studio for Windows, there is also a bug in the Android SDK build
tools that you need to fix in order to properly create the classes.dex files when building your
project.
https://riptutorial.com/ 50
Go to your Android SDK folder, open the build-tools folder and there will be folders with the
numbers of the Android SDK compilers, such as:
C:\android-sdk\build-tools\23.0.3\
C:\android-sdk\build-tools\24.0.1\
C:\android-sdk\build-tools\25.0.2\
Inside each of those folders, there is a file called mainClassesDex.bat, a batch script used to
create the classes.dex files. Open each mainClassesDex.bat file with a text editor (Notepad or
Notepad++) and in its script, find and replace the block:
SET params=%params:'=%
if DEFINED output goto redirect
call "%java_exe%" -Djava.ext.dirs="%frameworkdir%" com.android.multidex.MainDexListBuilder
%disableKeepAnnotated% "%tmpJar%" %params%
goto afterClassReferenceListBuilder
:redirect
call "%java_exe%" -Djava.ext.dirs="%frameworkdir%" com.android.multidex.MainDexListBuilder
%disableKeepAnnotated% "%tmpJar%" %params% 1>"%output%"
:afterClassReferenceListBuilder
Source here.
After the steps above, you should be able to successfully build your Xamarin.Android app with
MultiDex.
ProGuard is a tool used in the building process to optimize and obfuscate the Java code of your
APK, and also remove unused classes. The resulting APK when using ProGuard will have a
smaller size and will be harder to reverse-engineer (decompilation).
ProGuard can be used too in Xamarin.Android apps, and also will reduce the APK file size and
obfuscate the Java code. Be aware, however, that the ProGuard obfuscation applies only to Java
code. To obfuscate .NET code, the developer should use Dotfuscator or similar tools.
https://riptutorial.com/ 51
How to use ProGuard in your Xamarin.Android app
First, to enable ProGuard in your Xamarin.Android app, go to your project Properties -> Android
Options -> Packaging -> Enable ProGuard, as in the print screen below:
Xamarin.Android, by default, sets its own configurations for ProGuard, that can be found inside the
folders obj/Debug/proguard or obj/Release/proguard, in the files proguard_project_primary.cfg,
proguard_project_references.cfg and proguard_xamarin.cfg. The three files are combined as
configurations for ProGuard and they are automatically created by Xamarin when building.
If the developer wishes to further customize the ProGuard options, he/she can create a file in the
project's root named proguard.cfg (other names are valid too, as long as the extension is .cfg) and
setting its Build Action to ProguardConfiguration, as in the picture below:
https://riptutorial.com/ 52
In the file, custom ProGuard options can be inserted, such as -dontwarn, -keep class and others.
Important
As by now (April/2017), the Android SDK that is usually downloaded has an old version of
ProGuard, which can cause errors when building the app using Java 1.8. When building, the Error
List shows the following message:
Error
Can't read [C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\MonoAndroid\v7.0\mono.android.jar]
(Can't process class [android/app/ActivityTracker.class] (Unsupported class version number
[52.0] (maximum 51.0, Java 1.7))) [CREATEMULTIDEXMAINDEXCLASSLIST]
Source here.
To fix this problem, you must download the most recent version of ProGuard (here) and copy the
contents of the .zip file to android-sdk\tools\proguard\. That will update the ProGuard and building
process should run without problems.
After that, you should be able to successfully build your Xamarin.Android app with ProGuard.
You made a great app and tested it in Debug, with good results. Everything was working fine!
But then, you decided to prepare your app for release. You set up MultiDex, ProGuard and Linker,
and then, it stopped working.
This tutorial aims to help you to find out common problems related to ProGuard and Linker that
can cause mysterious bugs.
https://riptutorial.com/ 53
Understanding Xamarin.Linker
Xamarin.Linker is a tool in the building process that removes unused code and classes from your
.NET code (not Java code). In your project's Properties -> Android Options -> Linker, there will
be an selection box Linking with the options:
Sdk Assemblies Only: This option makes the Xamarin.Linker to check for unused code only in
the Xamarin libraries. This option is safe.
Sdk and User Assemblies: This option makes the Xamarin.Linker to check for unused code in
the Xamarin libraries and in the project code (including PCLs, Xamarin components and NuGet
packages). This option is not always safe!
When using Sdk and User Assemblies option, Xamarin.Linker may think that parts of the code are
unused when actually they are very much used! That may cause some libraries to stop working
properly and cause bugs in your app.
https://riptutorial.com/ 54
In the example below, using Xamarin.Linker caused a NuGet Package (Octokit) that works fine to
stop working, because it could not connect to the internet anymore:
[0:] ERROR
[0:] SOURCE: mscorlib
[0:] MESSAGE: Object reference not set to an instance of an object.
[0:] STACK TRACE: at Octokit.PocoJsonSerializerStrategy.DeserializeObject (System.Object
value, System.Type type) [0x003d8] in D:\repos\octokit.net\Octokit\SimpleJson.cs:1472
at Octokit.Internal.SimpleJsonSerializer+GitHubSerializerStrategy.DeserializeObject
(System.Object value, System.Type type) [0x001c3] in
D:\repos\octokit.net\Octokit\Http\SimpleJsonSerializer.cs:165
at Octokit.SimpleJson.DeserializeObject (System.String json, System.Type type,
Octokit.IJsonSerializerStrategy jsonSerializerStrategy) [0x00007] in
D:\repos\octokit.net\Octokit\SimpleJson.cs:583
at Octokit.SimpleJson.DeserializeObject[T] (System.String json,
Octokit.IJsonSerializerStrategy jsonSerializerStrategy) [0x00000] in
D:\repos\octokit.net\Octokit\SimpleJson.cs:595
at Octokit.Internal.SimpleJsonSerializer.Deserialize[T] (System.String json) [0x00000] in
D:\repos\octokit.net\Octokit\Http\SimpleJsonSerializer.cs:21
at Octokit.Internal.JsonHttpPipeline.DeserializeResponse[T] (Octokit.IResponse response)
[0x000a7] in D:\repos\octokit.net\Octokit\Http\JsonHttpPipeline.cs:62
at Octokit.Connection+<Run>d__54`1[T].MoveNext () [0x0009c] in
D:\repos\octokit.net\Octokit\Http\Connection.cs:574
--- End of stack trace from previous location where exception was thrown ---
To make the library start working again, it was necessary to add the package reference name in
the Skip linking assemblies field, located in project -> Properties -> Android Options -> Linker, as
in the picture below:
Xamarin.Linker perceives as unused code mostly code from model classes in your project's core.
https://riptutorial.com/ 55
To make the class preserved during the linking process, you can use the Preserve attribute.
First, create in your project core's a class named PreserveAttribute.cs, insert the following code
and replace the namespace with your project's namespace:
PreserveAttribute.cs:
namespace My_App_Core.Models
{
public sealed class PreserveAttribute : System.Attribute
{
public bool AllMembers;
public bool Conditional;
}
}
In each model class of your project's core, insert the Preserve attribute as in the example below:
Country.cs:
using System;
using System.Collections.Generic;
namespace My_App_Core.Models
{
[Preserve(AllMembers = true)]
public class Country
{
public String name { get; set; }
public String ISOcode { get; set; }
[Preserve(AllMembers = true)]
public Country(String name, String ISOCode)
{
this.name = name;
this.ISOCode = ISOCode;
}
}
}
After that, the linking process will not remove the preserved code anymore.
Understanding ProGuard
ProGuard is a tool in the building process that removes unused code and classes from your Java
code. It also obfuscates and optimizes the code.
However, ProGuard sometimes may remove code that it perceives as unused, when it is not. To
avoid that, the developer must debug the app (in Android Device Monitor and in the Visual Studio
Debug) and detect which class was removed, for then to configure the ProGuard configuration file
to keep the class.
Example
https://riptutorial.com/ 56
In the example below, ProGuard removed two classes
(Android.Support.V7.Widget.FitWindowsLinearLayout and
Android.Support.Design.Widget.AppBarLayout) used in AXML layout files, but that were perceived
as unused in the code. The removal caused ClassNotFoundException in the Java code when
rendering the activity layout:
layout_activitymain.axml:
https://riptutorial.com/ 57
To fix this error, it was necessary to add the following lines to the ProGuard configuration file of the
project:
After that, no more errors were shown when creating the layout.
ProGuard Warnings
ProGuard sometimes show warnings in the Error List after building your project. Although they
raise a question of whether your app is OK or not, not all of their warnings indicate troubles,
especially if your app successfully builds.
One example for that is when using the Picasso library: when using ProGuard, this may show
warnings such as okio.Okio: can't find referenced class (...) or can't write resource [META-
https://riptutorial.com/ 58
INF/MANIFEST.MF] (Duplicate zip entry [okhttp.jar:META-INF/MANIFEST.MF]) (...), but the app builds
and the library works without problems.
https://riptutorial.com/ 59
Chapter 10: RecyclerView
Examples
RecyclerView Basics
This is an example of using Android Support Library V7 RecyclerView. Support libraries are
generally recommended because they provide backward-compatible versions of new features,
provide useful UI elements that are not included in the framework, and provide a range of utilities
that apps can draw on.
To get the RecyclerView, we will install the necessary Nuget packages. First, we will search for v7
recyclerview. Scroll down until we see Xamarin Android Support Library - v7 RecyclerView. Select it
and click Add Package.
https://riptutorial.com/ 60
https://riptutorial.com/ 61
is available as a Xamarin component. In order to add the component, right-click on Components
within the Android project in Solution explorer and click on Get More Components.
Within the Component Store window that appears, search for RecyclerView. In the search list,
select Android Support Library V7 RecyclerView. Then click on Add to App. The component gets
added to the project.
Next step is to add the RecyclerView to a page. Within the axml (layout) file, we can add
RecyclerView as below.
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RecyclerView requires at least two helper classes to be set-up for basic standard implementation
viz: Adapter and ViewHolder. Adapter inflates item layouts and binds data to views that are displayed
within a RecyclerView. ViewHolder looks up and stores view references. The view holder also
helps with detecting item-view clicks.
https://riptutorial.com/ 62
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int
viewType)
{
// set the view's size, margins, paddings and layout parameters
var tv = new TextView (parent.Context);
tv.SetWidth (200);
tv.Text = "";
In the OnCreateViewHolder method we first inflate a View and create an instance of the ViewHolder
class. This instance has to be returned. This method is invoked by the Adapter when it requires a
new instance of ViewHolder. This method won't be invoked for every single cell. Once
RecyclerView has enough cells to fill the View, it will re-use the old cells that is scrolled out of the
View for further cells.
The OnBindViewHolder callback is invoked by Adapter to display the data at the specified position.
This method should update the contents of the itemView to reflect the item at the given position.
Since the cell contains just a single TextView, we can have a simple ViewHolder as below.
RecyclerView mRecyclerView;
MyAdapter mAdapter;
https://riptutorial.com/ 63
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
mRecyclerView = FindViewById<RecyclerView> (Resource.Id.recyclerView);
string[] GetData()
{
string[] data;
.
.
.
return data;
}
LayoutManager class is responsible for measuring and positioning item views within a
RecyclerView as well as determining the policy for when to recycle item views that are no longer
visible to the user. Before the RecyclerView, we had to use ListView to arrange cells in a s in a
vertically scrolling list and GridViewto display items in a two-dimensional, scrollable grid. But now
we can achieve both with RecyclerView by setting a different LayoutManger. LinearLayoutManager
arranges cells as in a ListView and GridLayoutManager arranges cells Grid fashion.
In Android Java, the way to set up a listener for a Click is using a onClickListener for the view
that will be clicked, like this:
However, in Xamarin.Android, the way to set up a listener for a Click event is by adding a
EventHandler, in the following ways:
1.
https://riptutorial.com/ 64
picture.Click += delegate {
// do stuff
};
2.
3.
Note that the EventHandler is added, not set. If the Click EventHandler is added inside a
GetView method from a GridView/ListView adapter, or a OnBindViewHolder method from a
RecyclerView.Adapter, every time that the item view is created a new EventHandler will be added.
After scrolling several times, multiple EventHandlers will be added, and when the view gets
clicked, all of them will be fired.
To avoid this trouble, the EventHandlers must be unsubscribed and subscribed subsequently in
the GetView or OnBindViewHolder methods. Also, they must use the number 3. way to set the
EventHandler, otherwise it will not be possible to unsubscribe the EventHandlers.
https://riptutorial.com/ 65
public AdapterPersons(Context context, Android.Support.V7.Widget.RecyclerView
recyclerView, List<Person> persons)
{
this.context = context;
this.recyclerView = recyclerView;
this.persons = persons;
}
https://riptutorial.com/ 66
Chapter 11: Toasts
Examples
Basic Toast Message
First, instantiate a Toast object with one of the MakeText() methods. This method takes three
parameters: the application Context, the text message, and the duration for the toast. It returns a
properly initialized Toast object. You can display the toast notification with Show(), as shown in the
following example:
This example demonstrates everything you need for most toast notifications. You should rarely
need anything else. You may, however, want to position the toast differently or even use your own
layout instead of a simple text message. The following sections describe how you can do these
things.
You can also chain your methods, call as a one-liner and avoid holding on to the Toast object, like
this:
For more information refer to the more complete Android documentation on the topic.
Sometimes we want to give extra information to our user with colors (for example red means
something wrong has happened) We can change toast message background color using setting a
color filter to the view which our toast give us (here I use a ColorMatrixColorFilter):
https://riptutorial.com/ 67
And also we can change the text color if background is light or dark:
We can change our toast using SetGravity method. This method takes three parameters: first is
gravity of toast on screen and two others set toast offset from the starting position (which is set by
the first parameter):
https://riptutorial.com/ 68
Chapter 12: Xamarin.Android - Bluetooth
communication
Introduction
In Xamarin.Android the BluetoothSocket.InputStream and BluetoothSocket.OutputStream
properties are by design automatically converted to System.IO.Stream. In case of so called
interactive communication protocol, when server responds only when client talks to it,
System.IO.Stream is not good because it has no method or property to get the number of
available response bytes before reading the response.
Parameters
Parameter Details
Examples
Send and receive data from and to bluetooth device using socket
byte[] Talk2BTsocket(BluetoothSocket socket, byte[] cmd, Mutex _mx, int timeOut = 150)
{
var buf = new byte[0x20];
_mx.WaitOne();
try
{
https://riptutorial.com/ 69
using (var ost = socket.OutputStream)
{
var _ost = (ost as OutputStreamInvoker).BaseOutputStream;
_ost.Write(cmd, 0, cmd.Length);
}
// needed because when skipped, it can cause no or invalid data on input stream
Thread.Sleep(timeOut);
return buf;
}
https://riptutorial.com/ 70
Chapter 13: Xamarin.Android - How to create
a toolbar
Remarks
Dear Team,
I think that its good to mention about official Android documentation where toolbar control is
explained in details:
https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
There is also interested content about Android.Support.v7 library used in the sample:
https://developer.android.com/training/appbar/index.html
Examples
Add toolbar to the Xamarin.Android application
In the "values" folder under "Resources" add new xml file called "styles.xml":
https://riptutorial.com/ 71
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-
palette-->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<item name="colorControlHighlight">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
Next step is to add "toolbar.axml" file that contains toolbar control definition to the "layout" folder:
Now please open "Main.axml" file and add below code just below closing tag for the first layout.
Your code should look like below:
</LinearLayout>
Now you have to add information about theme that your app uses. Open "AndroidManifest" file
and add theme information to the "application" tag:
https://riptutorial.com/ 72
Last step is to connect the toolbar in Activity file. Open "MainActivity.cs" file. You have to change
derivation from "Activity" to "AppCompatActivity". Now get reference to the toolbar and set it as
default toolbar for the activity in the "OnCreate" method. You can also define title:
https://riptutorial.com/ 73
Read Xamarin.Android - How to create a toolbar online: https://riptutorial.com/xamarin-
android/topic/4755/xamarin-android---how-to-create-a-toolbar
https://riptutorial.com/ 74
Credits
S.
Chapters Contributors
No
Getting started with Amy Burns, Community, Jon Douglas, Kevin Montrose, Ryan
1
Xamarin.Android Weaver
App lifecycle -
2 CDrosos, Daniel Krzyczkowski, Steven Mark Ford
Xamarin.Andorid
Barcode scanning
3 using ZXing library in GvSharma
Xamarin Applications
Publishing your
8 Xamarin.Android Alexandre
APK
Xamarin.Android -
11 Bluetooth Ladislav
communication
Xamarin.Android -
12 How to create a Daniel Krzyczkowski, tylerjgarland
toolbar
https://riptutorial.com/ 75