Posts

Showing posts with the label Xamarin.Android

Get started with SQLite in from Xamarin.Forms

Image
Get started with SQLite in from Xamarin.Forms Today I demonstrate how we can leverage sqlite to save data on mobile which can be useful for local application or to store cached data. In this post, I will show how we can use Sqlite from Xamarin.Forms in an Android application. This post will be composed by 3 parts: 1. Get Sqlite.net 2. Dependency service and database path 3. Use Sqlite from Xamarin.Forms 1. Get Sqlite The library that I always use is SQLite.net-pcl from praeclarum https://www.nuget.org/packages/sqlite-net-pcl/ .To not be confused with another SQLite.Net-PCL which is a fork of the original. To get started, simply install Sqlite in the Android project and in the Xamarin.Forms project. Once installed, Sqlite namespace should be accessible. And we should also have access to SqliteConnection . In order to start a connection, we need to provide a database path. The database path is platform specific. It is usually stored in a private folder of the application and is not...

Use Font Awesome from Xamarin.Android for your Xamarin.Forms project

Image
Use Font Awesome from your Xamarin.Forms project Icons are important in mobile applications where space is limited. We use icon everywhere to convey action intent like + would be to add an item or a bin would be to delete one. There are two ways to add icons to our mobile app: with images with fonts Today we will see the second option - how we can add Font awesome to our Xamarin.Android project and how we can use it from Xamarin.Forms. This post will be composed by three parts: 1. Why Font Awesome 2. Add Font Awesome to the Droid project 3. Use with Xamarin.Forms 1. Why Font Awesome If you are familiar with Web development, you must have encountered Font awesome http://fontawesome.io/ . It is an icon font containing lots of icon for every type of usage. The advantaged of using a font is that we can leverage the text options, like text color or text size, to easily update the style of the icon to match our app. Let’s see how we can bring Font Awesome to the Droid pro...

Transform an operation from a Xamarin.Android activity to an awaitable task

Image
Transform an operation from a Xamarin.Android activity to an awaitable task Today I want to share a neat trick which I have been using a lot - transform an asynchronous operation (triggered with StartActivityFromResult) from an Android Activity to an awaitable task - which then can be used with async await keywords in a Xamarin.Forms PCL project. Problem CustomPickActivity TaskCompletionSource in service 1. Problem In order to pick a file from the device local storage and execute an action using the selected file, the pick action (StartActivityFromResult/OnActivityResult) must be turned to an awaitable Task. 2. PickActivity To pick a file from our Xamarin.Forms project, we use a service which starts a picker activity. public class FilePicked : EventArgs { public string AbsolutePath { get; set; } } [Activity] public class PickActivity: Activity { static event EventHandler<FilePicked> OnFilePicked; protected override void OnCreate(Bundle savedInstanceState...

Differences between Internal and External Folders in Xamarin.Android

Differences between Internal and External Folders in Xamarin.Android and Xamarin.Forms When using Xamarin.Forms and Xamarin.Android, chances are that you had at one point of time to access a local file. Whether it is an image or a text file or even a local sqlite database, accessing a file from a Xamarin.Android project can be confusing. Today I will show you how you can get the absolute path of the different folders available in Xamarin.Android and we will see how we can get the paths from Xamarin.Forms. This post is composed 3 parts: 1. Internal folder 2. Public and private external folders 3. Access folder paths from Xamarin.Forms 1. Internal folder The internal folder is a folder specific to your application. It can only be access within your application and no other applications can touch the files within that folder. You can get it using the following: Android.App.Application.Context.FilesDir.AbsolutePath The path looks something like that: /data/user/0/com.kimserey.m...

Fix adb server is out of date killing... Android

Fix adb server is out of date killing... Last week I showed you how to fix the error adb server version doesn't match... with Xamarin.Android - https://kimsereyblog.blogspot.co.uk/2016/11/fix-installfailed-error-adb-server.html when trying to deploy an application built in Xamarin.Studio to a Genymotion VM. This was caused by two different versions of ADB being installed on the machine. One installed during Xamarin installation and another one installed separately. The solution was to set the ADB path in the options of Genymotion. I thought all was good but turns out there was another issue. 1. Problem When running adb shell , I had the following error in bash: adb server is out of date killing... 2. Solution Again the problem was due to my machine having two adb versions and the adb location in $PATH was the wrong one. In order to check verify that, I ran echo $PATH and saw that the path was incorrect. In order to rectify that, I modified the ~/.bash_profile which is ...

How to use the Snackbar API in Xamarin.Android and Xamarin.Forms

Image
Use Android Snackbar for your Xamarin Android project from Xamarin Forms The Snackbar API is a sleek way to provide notifications in Android. Here’s how it look like: You might have noticed the usage of it in applications that you use daily like Chrome, get the notification that the tab was closed. This is the snackbar. Today we will see how to use the snack bar api with Xamarin Android and how we can call it from our Xamarin Forms project. This post is composed by 3 parts: 1. When is the snackbar useful 2. Implement the snackbar api 2. Call it from Xamarin.Forms 1. When is the Snackbar useful The snackbar can be use to notify the user that something had happen. You would pass it a message which would popup for few seconds to acknowledge the user action. Another useful scenario for the snackbar is to provide an instant non obstrusive way to rectify an error by providing an action button on the right . The most common action is the undo action which allows the user to rectify ...

Fix INSTALL_FAILED error - adb server version doesn't match in Xamarin.Android

Fix INSTALL_FAILED adb server version doesn’t match Xamarin.Android Today I would like to share with you how you can fix the following problem: INSTALL_FAILED /Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.Debugging.targets: Warning: error: could not install *smartsocket* listener: Address already in use ADB server didn't ACK * failed to start daemon * error: cannot connect to daemon List of devices attached adb server version (32) doesn't match this client (36); killing... 1. Problem You might be running two different ADB and the versions collide. 2. Solution Verify the ADB version used by Xamarin: About Xamarin > Show details > Android SDK: [PATH] Make sure the PATH is the same as the one referenced in Genymotion. Genymotion > Settings > ADB > Use custom Android SDK tools > Put PATH in Android SDK This means that the adb used by Xamarin Studio to perform all deployment commands will be the same as the dae...

How to fix Xamarin Android app instantly closing with error after deployment

Image
Xamarin.Android app instantly closing with error after deployment Last week I had an issue suddenly after updating Xamarin.Android and downloading the latest Android sdk 24. My application kept closing instantely after being deployed showing the following error: Unfortunately, [App name] has stopped. I looked online and all I found was answers which recommended to uninstall the app either through the Android settings or using adb uninstall <package name> , clean and rebuild but nothing did it . It was still crashing. 1. Scoping the issue I wasn’t sure what was going on until I remembered that I could access the logs via adb logcat . After inspecting the logs, I saw the following error message: AndroidRuntime: java.lang.RuntimeException: Unable to get provider mono.MonoRuntimeProvider: java.lang.RuntimeException: Unable to find application Mono.Android.Platform.ApiLevel_24 or Xamarin.Android.Platform! It was that it couldn’t find ApiLevel_24 but my manifest was configure...

Build your own Line chart for Xamarin.Forms (Part 2)

Image
Build your own Line chart (Part 2) Last week we saw how we could use custom renderers with boxview to draw via the canvas api https://kimsereyblog.blogspot.co.uk/2016/10/build-your-own-line-chart-for.html . Today I will go through the steps to draw a line chart supporting markers. The line chart is very simple and has only one objective, give a rough indication of the current trend of the data displayed. In order to draw the chart, we will divide the chart in four layer which we will draw one by one. Therefore this post will be composed by the three layers. Draw background and bands Draw axis and labels Draw lines and markers 1. Draw background and bands We can see the canvas as a painting canvas. When we draw something and then draw something on top of that, the last thing drawn will hide the previous one - just like as if you are painting something on top of something else. Therefore the first thing we need to draw is the background. // Draws background paint.Color = Colo...

Build your own Line chart for Xamarin.Forms (Part 1)

Image
Build your own Line chart for Xamarin.Forms with Custom renderers (Part 1) Last week I needed a line chart to plot expenses. I had two choices: use an existing library or draw the chart myself on canvas. I decided to go for the second - draw the chart using the Android Canvas API. I chose this approach because it gives me full flexibility to create a style and behaviour that match perfectly my application. Here’s the chart result: This tutorial will be composed by four points divided in two parts: 1. Xamarin.Forms custom renderer (Part 1) 2. Explore the Android Canvas API (Part 1) 3. Create a GraphView which will draw a line chart (Part 2) 4. Use it (Part 2) 1. Xamarin.Forms custom renderer Officiel documentation: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/ A custom renderer is a class used by Xamarin.Forms to define platform specific behaviours of your views. In this tutorial, we will be using a custom renderer to access the Android native Canvas API ...

What does DP, DPI, PPI and PX represent in Android?

What does DP, DPI, PPI and PX represent in Android? I’ve been playing with Xamarin Android for a while now and one thing that disturbed me when I started was the units of measure. What is the difference between DP, DPI, PPI and PX in Android? I found my answers in the Material design guide and today I would like to share my undertanding of the differences and bring a different explanation which hopefully will help you get a better understanding. This post is composed by three parts: 1. PX - pixels 2. DPI / PPI - pixels per inch 3. DP - density-independent pixel 1. PX - pixels Pixels is the most granular unit of measure. When talking about resolution 1920x1080, 2560x1440, etc… It represents the amount of pixels which fit in the screen. The higher the better but a second aspect equally important has too be taken in consideration - the screen dimension . Two devices with the same resolution but different screen sizes will not have the same display. For a similar resolution, th...

How to make a splash screen with Xamarin.Android

Image
How to make a splash screen with Xamarin.Android The first time I had to implement a splash screen for a Xamarin.Android app, I was completely lost. Xamarin official documentation is great https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/ but without prior knowledge in Android, it is hard to follow. So today I would like to show you how you can create a splash screen for an Xamarin.Android app and provide more explicit information on what is happening and why is it done this way. This post is composed by 3 parts. 1. What's a splash screen 2. Implement the splash screen 3. Use the splas screen A full sample is available on my GitHub https://github.com/Kimserey/FileFoldersXamarin/tree/master . 1. What’s a splash screen A splash screen is a screen shown to the user while the application is loading in the background. Xamarin.Forms applications take few seconds before being completely loaded, therefore it is important to provide a splash scr...