Tuesday, April 7, 2015

iOS WebView insets

I have been battling some UI constraints on iOS and I have finally found a solution to how to solve this specific problem and just wanted to share.

My problem was that I had a UIWebView, which kept laying itself out underneath the NavigationBar in my controller. A quick fix would be to just set edges for the extended layout to none like so:



However, this will make you lose the nice effect of views scrolling behind the NavigationBar, for instance if the web page you are displaying scrolls.
Enter insets. As the name kind of indicates you add some spacing into your view. There is a property called AutomaticallyAdjustsScrollViewInsets. However, for some reason it does not do anything in my case, so I had to manually adjust the inset, which I did in ViewWillLayoutSubviews as in ViewDidLoad the TopLayoutGuide is not ready yet and will give you 0 for its Length. Basically this is what I had to do:



This tells both the UIWebView's internal ScrollView and the scroll bar that you want some space in the top equals to the height of the NavigationBar.

Friday, January 30, 2015

Changing from Map property to GetMapAsync() on MapFragment

Recently the Map property on the MapFragment was marked obsolete. Instead, we now have to use the GetMapAsync() method. However, that method expects a callback object. Hence, we need slightly more code to not have our code use obsolete stuff.

For the callback we need to implement the IOnMapReadyCallback interface, which simply has a method which gives us the GoogleMap instance.


As you can see I simply implemented that interface, and as all interfaces coming from the Java world which expects a Dispose() and Handle implementation we need to inherit from Java.Lang.Object. I also added an event to that class instead of passing a reference of my Fragment into that class.


Now you might be using the SetupIfNeeded() method which Xamarin also uses in their samples and is called in OnCreate() and OnResume() and where ever you feel like it. For that purpose I have adapted that to use the callback class.


Tuesday, January 27, 2015

Crazy start of 2015. M.Sc., MVP and new apartment++

End of 2014 and this month have been a very busy time for me. I have been working very hard on finishing up my Masters Thesis, "Environmental Sensor Monitoring tablet application designed using cross-platform design patterns and frameworks", which in short terms concerns creating cross-platform applications using C#, Windows 8.1 SDK and the Xamarin SDK, using some design patterns and frameworks which help sharing code across the targeted platforms. The thesis was handed in the 19th of January and defended today 27th of January. I managed to get an A for the project, which I am super happy about.

Brüel & Kjær which I made the thesis in collaboration with and where I have been working part time the past 3.5 years also offered me a more permanent position last year, which I have accepted, which means I am starting full time from February, continuing creating cross-platform apps using Xamarin.

Alongside the thesis, I've been in the middle of buying an apartment, which is almost settled now. So the plan is that I am going to move 1st of March, which is going to be great. Right now I am living in a small dorm room, where I just have enough room for a desk, bed and some storage furniture. It is going to be great to get around 8 times the space of the dorm room.

Last but not least, I have been awarded a Xamarin Most Valuable Professional (MVP) award, which was announced a couple of days ago. I am really honored to get such an award and this is my first MVP award ever. Hopefully I will live up to the award and get a new one at the end of the year.

Looking forward to tackle this year and get the best out of it, get cracking at some code and provide answers to your questions on the community forums etc. I am planning on creating more tutorial videos, like I have done previously, which seem to be quite popular. If you have any topics you want covered related to Xamarin, do post a comment and I might just make a video on that.

Thursday, November 6, 2014

Using MapFragment inside ScrollView

I have an app where I need to put a Map inside a ScrollView, doing so will make the ScrollView intercept all the touch events. So the solution is to call RequestDisallowInterceptTouchEvent(true) whenever you need the Map to scroll around, pinching it etc. The problem is that MapFragment or its properties do not expose any touch events, so it makes it a bit harder to find out when it is touched.

I found a nice little code snippet, Lorensius Londa posted on his blog, for Java Android, which overlays the MapFragment with a FrameLayout and using the FrameLayout touch event. Using this exact technique help me achieve my goal as well. Here is the code I used.

So as you see in the code there is a TouchableWrapper which simply fires some Actions whenever it was touched or the touch was released. This is grabbed by the TouchableMapFragment which subsequently fires the equivalent events.
The Fragment is used as follows in a AXML layout.
Then you can use the events in your Activity like so.
Now you should be able to navigate your Map when it is nested in a ScrollView.

Friday, October 31, 2014

Some experiences with the new Support packages on NuGet

I spent some time moving over to the new Support packages Xamarin released on NuGet, instead of using the versions from the Component store.

Jonathan Dick made a nice post about what makes the packages on NuGet a bit more smarter than they were previously, which I find very interesting. However, it is not all happy times as there seems to be some caveats by using these packages.

Hence, here are some findings that you might encounter when using them.

I have some Android Library projects, each updated to the new NuGet packages. The Android Library projects seem all to have $(TargetFrameworkVersion) defined in the .csproj file, which comes from the setting in the project properties in the Application tab called Compile using Android version. This sets the $(TargetFrameworkVersion) in the project file. However in my Android Application projects, I previously had that same option set to Use Latest Platform. The problem with this, is that it does not seem to explicitly set that in the csproj file. This gives some problems when fetching the NuGet for the Application project, where NuGet might tell you that it is incompatible and simply not install it (not cool :( ).

Another problem is that, say you have an Android Library project using Google Play services, this is also a NuGet package, which additionally brings in a lot of Support packages (including v4, v7, v13 (whoa!)). Now you do all your stuff with the Play services inside of the library, so you think you can suffice with simply adding the needed Support packages in your Android Application project. Lets say you only need Fragment backwards compatibility and you just pull in Support v4. This will give you a headache and throw a bunch of errors telling you that you have duplicate managed types.
The solution seems to match the packages you added in the Android Library project. So if you are using Google Play services in your Android Library project, you need the exact same packages in your Android Application project.

Apart from these couple of things, it seems that the new packages work pretty well. It also seems that additional Support v7 packages have made it into NuGet and more are to come!

I've switched the MvvmCross "3.5" branch and my own MvxPlugins "threeandahalf" branch to use this new stuff.

Friday, October 3, 2014

Getting User Location to work in iOS8

There were some minor changes to how iOS8 works with core location. Previously when requesting the user's location on the MapView with ShowsUserLocation a nice pop-up would automagically show up asking the user if this was ok. Apple decided to make this a bit more flexible, allowing the developers to enter their own message. So if you compile your pre-iOS 8 app with the new SDK this might break, since you now have to manually call a method to get that pop-up to show.

So the quick fix is to do the following:

1. In your info.plist file add one of the following key/value pairs:

2. I then have a Extensions.cs where I keep my various extensions, for this case I've just made a couple of checks to whether the selectors are available on the platform, so the code won't crash on previous versions of iOS.

3. Remove your previous call to mapView.ShowsUserLocation and do the following instead:

Now you respond to the AuthorizationChanged Event and set the ShosUserLocation flag accordingly.


Edit (3/10/2014 17:00):
Now that I have had the chance to play a bit more with it, I have a couple of observations that might help you. First don't let go of the CLLocationManager instance, i.e. have it as a class field otherwise the dialog will disappear pretty quickly and it won't let you get location updates.

I didn't mention that you will have to do some checks on what iOS version you run at now and only call this code when on iOS 8 and higher, to do this you can do something as follows.

Saturday, July 5, 2014

Port of Floating Action Button

Android L has introduced something called a Floating Action Button (FAB), which is basically a circle overlayed on top your application. This FAB can be clicked to do an action. In the Google+ application this is used to persistently allow the user to create a new post.


It basically looks like in the screenshot above, where you can see a circle with a play icon at the bottom of the screen. Faiz Malkani backported this from Android L to work on JellyBean and KitKat, and I took the liberty to port it to Xamarin.Android.

You can find the repository on GitHub along with a sample application, which the screenshot above is from.