Full source code for this article can be downloaded on GitHub: HeaderedGridView.

For the easy import feature of Yarly, I needed to create a view that would easily allow a user to select an existing photo to import into the app or to take new photo. A grid view of photos from the camera roll but with the first item as a button that would load the camera would serve nicely for this use case. See the screen shot below of an early version of the UI:

Yarly Screen Shots 2014-04-20_194747_242000

This reminded me of the AddHeaderView method available on ListView. With that as a guide, I decided to add similar functionality to the GridView. Since I’m using MvvmCross, I also want to make sure that I can use bindings in the header control. While I’m at it, having to explicitly call AddHeaderView in my activity code is useful, but I’d much rather to be able to do this in markup.

Requirements

To sum up the above requirements:

  1. Ability to add a header to a GridView in xml markup
  2. The header is laid out inline with the rest of the items in the GridView
  3. The header scrolls with the rest of the content, i.e., it is not always visible
  4. The header must be able to use MvvmCross bindings

Code

A full sample project can be downloaded from my GitHub repo: HeaderedGridView.

In attrs.xml, add an attribute to use for our header.

<!-- attrs.xml -->
<declare-styleable name="GridView">

   <attr name="header" format="reference" />
</declare-styleable>

In the HeaderedGridView, all we need to do now is check for the presence of this attribute, and if it exists, inflate the view. Reading the header id is straight forward processing of the IAttributeSet. I use some helper classes to iterate and dispose of the attributes in a more C# way. The details aren’t very important, but the relevant classes are included in the sample solution. The MvvmCross class MvxAndroidBindingContextHelpers can return the current binding context which can be used to inflate and bind the header at the same time.

// HeaderedGridView.cs
private void ProcessAttrs(Context c, IAttributeSet attrs)
{
    _headerId = DEFAULT_HEADER_ID;
    using (var attributes = c.ObtainDisposableStyledAttributes(attrs, Resource.Styleable.GridView))
    {
        foreach (var a in attributes)
        {
            switch (a)
            {
                case Resource.Styleable.GridView_header:
                    _headerId = attributes.GetResourceId(a, DEFAULT_HEADER_ID);
                    break;
            }
        }
    }
}

private void LoadHeader()
{
    if (_headerId == DEFAULT_HEADER_ID) return;
    IMvxAndroidBindingContext bindingContext = MvxAndroidBindingContextHelpers.Current();
    _header = bindingContext.BindingInflate(_headerId, null);
}

Now that we have a header, we can wrap our current adapter in the HeaderViewListAdapter. As the name implies, this is the exact same adapter used by the ListView. It handles knowing when and where to show the header. In my sample code, I have the grid create the adapter directly, but this can work just as well if an adapter is passed in from outside.

// HeaderedGridView.cs 

private IListAdapter GetAdapter()
{
    var headerInfo = GetHeaders();
    ICursor cursor = ImageAdapter.CreateCursor(Context);
    IListAdapter adapter = new ImageAdapter(Context, cursor);

    if (headerInfo != null)
    {
        adapter = new HeaderViewListAdapter(headerInfo, null, adapter);
    }
    return adapter;
}

So I don’t have to hard code sizes and so the header matches the rest of the items in the grid, I set the height and width once the grid is being laid out. In the OnMeasure method we check if the header isn’t null, and if the ColumnWidth doesn’t match the previous column width we saw. Caching the width and testing this prevents us from setting the layout parameters when we don’t have to; OnMeasure is called multiple times.

// HeaderedGridView.cs
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

    if (_header != null && base.ColumnWidth != _cachedColumnWidth)
    {
        _cachedColumnWidth = base.ColumnWidth;
        _header.LayoutParameters = new ViewGroup.LayoutParams(_cachedColumnWidth, _cachedColumnWidth);
    }
}

Now all we need to do is include our grid in a layout. It’s almost exactly the same as the regular GridView, except we can now optionally specify a header. This is from the FirstView layout in the sample.

<!--FirstView.axml-->
<HeaderedGridView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1"
   android:numColumns="3"
   android:verticalSpacing="10dp"
   android:horizontalSpacing="10dp"
   android:stretchMode="columnWidth"
   android:gravity="center"
   android:fastScrollEnabled="true"
   android:background="#000000"
   local:header="@layout/gridheader" />

The gridheader layout is just a simple layout with a single image button button. Note that we are binding the click of the button to the ClickCommand in our view model.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
    xmlns:local=http://schemas.android.com/apk/res-auto
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <ImageButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#90C53E"
        android:src="@drawable/camera"
        local:MvxBind="Click ClickCommand" />
</LinearLayout>

Firing up the sample solution and we should see something similar to:

Future Improvements

My implementation restricts me to only being able to add one header. While one header is currently good enough for me, if I wanted to expand it add more, I could emulate ListView a bit more. Specifically the onFinishedInflate method which adds child layouts as a list of headers. Of course, I could also add an explicit AddHeaderView method that could be called multiple times.

I’ve ignored footers entirely. Again, this is just because I don’t currently have any need for a footer. They could be added easily enough following the same pattern as headers.

Happy Coding.

this post was originally on the MasterDevs Blog

I’ve spent the past few hours working through a fresh install of Xamarin Studio. While most of it is really easy, there are some major gotchas. The best thing about the process is that it is one installer for everything including dependencies. The download page misleadingly asks you what level license you want when. Ignore that. It has nothing to do with the installer or the license you actually wind up using. If you’re just trying it out there’s a 30 day trial license that gets you access to all of the features.

The installer pulls down all of the Android development tools as well: the emulator, the SDK manager, java samples. Everything. After the installer finishes you are ready to write your first android app. Almost.

Hello World in Xamarin Studio

First thing I did was fire up Xamarin Studio (their IDE) and create a new Android Application. Out of the box this is a “Hello World” app with a single button on a page. Couldn’t get much simpler. It didn’t compile for me. I got:

‘Error MSB4018: The “Aapt” task failed unexpectedly’ on new Android Application. No code added.

The solution here was fairly simple. Go to the Android SDK Manager (it should be in your start menu/screen) and install all of the updates. Just click the install button on the bottom right.

Once that’s done you can go back and run your app. It will prompt you to launch an emulator and deploy the app automatically. One thing to note here is that the emulator can take a while (minutes) to start up. Even worse, once it is fully started it stays on the ANDROID loading screen waiting for you to hit a button. There’s no way for you to know it’s ready for you unless you go back and poke it every now and then.

Debugging in Visual Studio

While Xamarin Studio seems to be a decent IDE and you can debug an application from there (breakpoints, watch screens, variable evaluation on mouse hover, threads list, etc.) I am definitely more comfortable with Visual Studio. If you don’t have a trial (or real) license yet, you will be prompted for it when you open up a Xamarin project in Visual Studio, either by opening the solution file you created in Xamarin Studio or by creating a new Android project directly in Visual Studio. That’s where it stops being easy though. The first thing you might notice is that when you try to debug the application you get the following error:

The application could not be started. Ensure that the application has been installed to the target device and has a launchable activity (MainLauncher = true).

Additionally, check Build->Configuration Manager to ensure this project is set to Deploy for this configuration.

The error message is right, the application wasn’t installed on the “target device” (the emulator). If you created the project in Xamarin Studio you need to explicitly tell VS that you want to deploy the app the phone before debugging. Go to the Configuration Manager and ensure that Deploy is checked off for your project.

If you created the project in Visual Studio, you shouldn’t have this problem. it seems to only happen when you create the project in Xamarin Studios first.

Now that the application is deployed to the phone there is still one more hurdle before you can debug from Visual Studio. Whenever I tried to attach to a running virtual machine the debugger would deploy the app and chug along for a little while. Eventually it would quietly die and disconnect. The application wouldn’t load in either the debugger or emulator. I didn’t see anything in the log file that helped point to the problem. Eventually I got lucky and determined that I could connect if the virtual machine was running an Intel Atom (x86) processor then everything worked. My assumption is that Visual Studio doesn’t know how to compile for ARM. If you look at the project properties for an Android app in Visual Studio, there is only x86 and x64:

Xamarin’s site has some pretty easy to follow instructions on how to configure an x86 emulator. The main task is to install Intel’s HAXM software. What they skip over is that you can’t install this if you have Microsoft Hyper-V installed. Hyper-V is Microsoft’s hardware virtualization stack that is used by the Windows Phone Emulator. Ben Armstrong has some really good instructions on how to create a profile in Windows that will disable Hyper-V at startup. They boil down to two commands to run on the command line and a restart:

bcdedit /copy {current} /d "No hypervisor"
bcdedit /set {GUID From the previous command} hypervisorlaunchtype off

For the GUID in the second command, copy the output of the first command including the brackets. It should look something like this: {7d067ad2-16ce-11e2-a059-9b573bf76ddc}. Then just restart the computer and select “No hypervisor” when prompted at the boot screen. From here you can continue through the instructions on how to configure an x86 emulator.

There’s a trade off here. You won’t be able to switch between debugging a Windows Phone application and an Android application without restarting your computer, but on the other hand, because it uses hardware level virtualization, the x86 emulator is significantly faster than the ARM versions.

UPDATE:

After running these two commands a couple of times, I got lazy and wrote a powershell script to do everything in one go:

# This script makes a copy of the current boot record and disables Hyper-V
# The next time the computer is restarted you can elect to run without Hyper-V
  
$output  = invoke-expression 'bcdedit /copy "{current}" /d "Hyper-V Disabled"'
$output -match '{.*}'
$guid = $matches[0]
$hyperVCommand = 'bcdedit /set "' + $guid + '" hypervisorlaunchtype off'
invoke-expression $hyperVCommand

Summary

Here’s a quick run down of the steps covered above

  1. Download and install Xamarin Studio
  2. Install all the updates in the Android SDK Manager
  3. Make sure Visual Studio knows to deploy the app before trying to debug
  4. Disable Hyper-V
  5. Configure an x86 emulator

this post was originally on the MasterDevs Blog

Trying to debug a new Windows Phone 8 application today I came across this problem. Whenever I hit F5, the emulator would start up like normal but Visual Studio would prompt me with a message box saying:

A specified communication resource (port) is already in use by another application.

Then the debugger would fail to start. After trying all of the obvious options and restarting everything in sight, I stumbled across a bit of advice which worked for me. Right click on your project and select Deploy. This will copy all necessary code to the emulator. After that you are free to hit F5 like normal.

this post was originally on the MasterDevs Blog

Windows Phone 8 comments edit

The Windows Phone 8 emulator depends on Hyper-V in order to run. The problem is that the consumer edition of Windows 8 doesn’t support Hyper-V. The easiest way I found to install it was to upgrade to Windows 8 Pro. Luckily with Windows 8 upgrading does not mean reinstalling. You can simply enter a new license key and you’re off to the races.

Now, none of this was obvious at least to me. Before figuring all this out I spent some time Googling and Binging. Hopefully you can use some of the information I gathered.

The first sign of trouble was when I installed the Windows Phone 8 SDK. If you don’t already have Hyper-V, you will see a cryptic warning that the SDK installer was not able to add you to the Hyper-V Administrators group.

Assuming this to be an important warning, I attempted to add myself the old fashioned way, through the Local Users and Groups snap-in. As it turns out though, that snap-in is not available on the standard edition of Windows 8. This should have been my first clue that something was amiss. Sadly, I didn’t pick up on it this early in the game.

Seeing myself at a bit of a dead end, I decided to move on and hope for the best. I opened up an existing Windows Phone 8 project in Visual Studio and hit debug. The next error message shed a light on why I couldn’t be added to the Hyper-V Administrators group; I didn’t have Hyper-V installed. It linked me to a somewhat useful MSDN article on the emulator’s requirements.

This article does a good job of explaining how to enable Hyper-V in your BIOS as well as Windows itself. It even tells you how to determine if your hardware can support Hyper-V. Not to repeat the article too much, but you can download Sysinternals’ Coreinfo tool and run it on the command line. Passing it the -v parameter will help minimize the noise in the output to just the fields you’re looking for. V stands for virtualization.

Now that I had verified that it wasn’t my hardware it was time to start looking at the software. To check what Windows features installed and/or disabled go to Programs and Features through the control panel and clicking on the “Turn Windows features on or off” link on the left hand side. Or even easier, bring up the search charm and type in “Turn Windows features on or off”. As you can see below, I didn’t have Hyper-V in my feature list.

To figure out how to get it installed, I started looking at my OS. I checked out Wikipedia to see what the different editions of Windows 8 were. Had I been smart enough to actually read the feature list at the bottom or the 3 sentence description of the editions, I would have seen that Hyper-V is only available in the 64 bit Pro and Enterprise editions. As it was, knowing that the official name of the consumer edition was simply (and confusingly) “Windows 8” was enough to verify which version of the OS came installed on my machine.

I already had an available Pro product key, so my next step was to try to upgrade as as painlessly as possible. As it turns out that was the easy part. To do this go the share charm and type in “add features to Windows 8”. You should see one option under settings. This will let you either buy a key or enter an existing one.

After you click next your Windows installation will upgrade itself which does take some time and a reboot. Now when we come back to the Turn Windows features on or off dialog Hyper-V is available but unchecked.

Simply check it off, click OK and wait for some more installation and reboots to happen.

Now I was finally able to go back to Visual Studio and hit debug again. I did miss one thing however. I still wasn’t in the Hyper-V Admin group. The emulator is smart enough to recognize that and offer to automatically fix it. Clicking OK on the prompt was enough to finish all the configuration I needed and finally show me my app:

this post was originally on the MasterDevs Blog