The App Foundation

The foundation of any app is very important. If you’ve been developing for a while you will have your own style and way of doing things in each app that you use. This is useful and helps to keep things in an order that you find easy to work with. If you’ve not been developing for that long, the task of getting into this kind of routine can be somewhat daunting. From personal experience, it wasn’t until I started programming in a professional capacity – I had been programming as a hobby for ten years previously – that I finally came to work out my own routine. I’ll admit that I did have a lot of help as well – working in an environment with other professional programmers and seeing their own styles is the best way of working your own styles out!

So to help others along (and to speed up development of my own apps) I have published a project that has the basics that every project that I write has. Below are some hints and tips for using it, but hopefully if you look at the code it’s mostly self explanatory! You can find the repository here.

There are a few main points I want to go over. The first is that I always have a base Activity and a base Fragment. By this I mean that I have two abstract classes that extend Activity and Fragment respectively. By doing this we can put in all the methods that we know we will want to access from all of our activities / fragments. The most important ones in the activity are setRootFragment and pushFragment. These two methods give us the ability to easily add new fragments without repeating code. They are probably my favourite methods out of all of them!

The second important methods are getLayoutId and onCreateWrappedView. The first method specifies the layout id that the fragment or activity should inflate. The second is used to do any UI setup for the activity or fragment. For example, if you inflate a layout that has a text view and you wish to assign the text view to a class wide field you would do something similar to:

public void onCreateWrappedView() {
globalTextView = (TextView) findViewById(;

In the fragment it is slightly different as we have to pass in the root view to be able to find the view in question – I think that’s fairly self explanatory, however!

There are some logging functions within the utils.Util class, which stop you having to put in an app tag every time. The class is also useful for putting other methods that are used app wide.

The final thing to note is the Api class. I’ve added a barebones api infrastructure using Retrofit2, which I use with a suite of managers (when I require it).




Word of the Week – Curmudgeon


According to the Oxford dictionary this means

a bad-tempered or surly person.

I think we all know someone to whom this word could be applied!


Raspberry Pi: Retropie Console

So as you may or may not know, the Raspberry Pi 2 came out early last year in February 2015. It’s taken a long while, but finally me and my partner (mainly my partner) got around to setting up Retropie on one.

The RetroPie project describes itself as “a collection of works that all have the overall goal to turn the Raspberry Pi into a dedicated retro-gaming console”, and it does this very well with support for early games consoles such as Dreamcast, PlayStation, and Nintendo64.

But enough of that; I’m sure you’re wondering how to go about getting it all set up!

Things you will need:

  1. A Raspberry Pi 2 (Model B)
  2. An SD card to put the RetroPie OS and game ROMs on
  3. A power supply for the Pi (of course!)
  4. A keyboard for setting up the Pi to begin with
  5. A screen with a HDMI port and cable
  6. A games controller to use with the Pi
  7. The Retropie software image

It’s important to note that if you’re using the controller linked to above, you’ll need to make sure you also have a USB wireless receiver for the xbox controller as well, as this is not included with the controller itself.

As you can see, I’ve linked to some of the products above that we personally have used, so we know that they work.

We start off by burning the image of the RetroPie software to the SD card so that we can then boot from it. We’re using Apple Macs here, so the software involved is called “ApplePie Baker” which takes the hassle out of burning the image correctly. If you’re using windows you can use something like the Win32DiskImager.

ApplePie Baker

With the SD card inserted into your mac select the IMG file and hit Restore Backup to burn the image to the SD card. This could take a while so you might want to go and grab a cup of tea (or a beer if it’s the evening!) whilst you wait.

Once that’s all done we can move on to putting the SD card into the Raspberry Pi, connecting the keyboard, screen and power supply and waiting for it to boot up. TO be perfectly honest, that’s almost all you need to do… However, sometimes you’ll need to change some of the setup options.

One thing that’s probably a good idea to do is expand the filesystem, which allows you to make full use of all the space on the Pi for adding game ROMs etc. Not only this but having WiFi is often preferable, so if you have a spare wifi adapter laying around you can use that too. To expand the filesystem you can use the steps described here. The instructions are slightly out of date so your setup might look a little different, but the general idea of where to go and what options to select are still correct.

Setting up WiFi can be found here, and is pretty simple really.

The final thing you’ll want to do is be able to transfer your newly curated ROMs to the Pi so that you can use them. There are three ways to do this, depending on whether you’ve connected your Pi to your local network via ethernet or WiFi. The first, that doesn’t require a network connection, is to use a USB drive:

  • (ensure that your USB is formatted to FAT32)
  • first create a folder called retropie on your USB stick
  • plug it into the pi and wait for it to finish blinking
  • pull the USB out and plug it into a computer
  • add the roms to their respective folders (in the retropie/roms folder)
  • plug it back into the raspberry pi
  • wait for it to finish blinking
  • refresh emulationstation by pressing F4, or choosing quit from the start menu

These steps are taken directly form the RetroPie project Github website.

The second and third methods of transferring ROMs requires some form of network connection. I won’t go into detail of the FTP method, as that’s not one that I have tried out. Instead I’ll show you how to transfer them over a file share, which is as simple as drag and drop once it’s setup.

If you’re on windows you can go into File Explorer by pressing the Windows Key + E, and in the address bar at the top type in \retropie which will bring up a list of folders on the Pi. If you’re on Mac simply open Finder, select the Go menu at the top of the screen, select Connect to Server and type in smb://retropie in the subsequent dialog box. From this, the folder you need to copy your ROMs to is aptly named ROMs.

I forgot to mention, there is one last step to all of this… HAVE FUN! Go ahead and test out your newly configured RetroPie console. There’s a lot of documentation on the web of what works and what doesn’t and more configuration that you can do if you want. But for now, I’ll leave you with this, and hope you have a retrotastic time!


Android Navigation View: Icons

In brief, I’ll be describing how to add icons to the Android NavigationView component, as well as changing the colours of those icons.

First we need to start off with the XML. You’ll need to start out with a DrawerLayout, and within that include your NavigationView.



		app:menu="@menu/activity_main_drawer" />


You’ll also notice that here I’ve included the a layout with id “app_bar_main”. This is just a layout for if you want to have a toolbar at the top of your app.

The important thing to notice here for getting our icons into the NavigationView is the app:menu attribute. This allows us to specify a menu resource, which has the information on what items we want to place in our menu. If you are using Android Studio you can find the “menu” folder within the “res” directory. Right clicking on this folder and selecting New > menu resource file. A typical menu resource will look similar to this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
	<group android:checkableBehavior="single">
		<item android:id="@+id/menu_home"

This XML is at the heart of adding items and icons to the menu. I’m sure from looking at this you can work out that the android:title attribute is the title to display in the NavigationView, and the android:icon attribute is similarly used to specify what drawable resource to use for the icon on the left hand side of the title.

However, this isn’t all. You may notice that if you select a coloured icon to be displayed (i.e. not just black and white) it will only display in black and white.This is because of the background tint that is automatically set on the items. To solve this, we need to obtain a reference to the icon in java code, and set the background tint for items to null.


It’s that simple!


IllegalStateException: The specified child already has a parent

To be precise, this error reads:

 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.

This is an error I recently came across when trying to get a recycler view to work. At first this can seem a bit confusing, but really it is very simple. Generally it means that you’ve forgotten one tiny detail in your onCreateViewHolder method.

When inflating the layout resource for your view holder, you’ll likely have a line similar to

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent);

This is all very well and good… except that using this version of the inflate() method actually tells the layout that it should attach itself to the layout parent… Instead we need to tell it not to do this by using

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);

The only difference here is we have added a parameter at the end; the boolean ‘false’. And that little parameter will solve all your problems! (well, perhaps not all, but at least one)


Google Play Game Services – Error 10002 (RESULT_SIGN_IN_FAILED)

One of the newest apps I recently worked on required the use of Google Play Game Services. There’s a lot of material from Google out there on how to set this all up and get it working, and in all honesty the code is all very simple. Just one problem.

After hours of trying, I could not get game services to work correctly and sign in. I had everything set up as described in the documentation, but it just wouldn’t work. After hours of googling, I finally ran across a one line throwaway comment that finally pointed me in the right direction.

Here is the documentation I used to setup everything in the Play Console. The one thing I unfortunately didn’t realise was that if you created the API Console Project using google’s old style console, even if you’re using the project on the new site, it will not work.

So, that said, the easiest fix was simply to remove the project from the game console and create a completely new one. You can do this by using the option entitled “I don’t use any Google APIs in my game yet”, which comes up after you click to add a new game.

Hopefully this will save someone a little bit of time!


Targeting the iPad – CSS

Sometimes we want websites to look different in certain platforms. In this case we will be looking at targeting iPads with CSS to make it look different from other platforms.

To do this we will be making use of media queries, which are usually used to target different screen sizes. Below are the relevant media queries required for the various iPads.

iPads in portrait and landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {

iPads in landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {

iPads in portrait:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {

iPad 3 & 4 Queries
If you want to target the iPad 3 & 4 then you’re in luck!

Retina iPads in portrait and landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (-webkit-min-device-pixel-ratio: 2) {

Retina iPads in landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio: 2) {

Retina iPads in portrait:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio: 2) {

iPad 1 & 2 Queries
Again, we can also specifically target iPads version 1 and 2.

iPads 1 & 2 in portrait & landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (-webkit-min-device-pixel-ratio: 1){

iPads 1 & 2 in landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio: 1)  {

iPads 1 & 2 in portrait:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio: 1) {

iPad Mini
Finally, this wouldn’t be complete without looking at the iPad Mini!

iPad mini in portrait & landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (-webkit-min-device-pixel-ratio: 1)  {

iPad mini in landscape:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio: 1)  {

iPad mini in portrait:

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio: 1)  {

Android – Styling the Contextual Action Mode

I recently found myself needing to use the Contextual Action Mode functionality of Android to make things look a little more “Androidy” (I was cloning an iOS app, at a customers behest.). All went well, except for one really annoying little UI problem. The left action button was slightly off in relation to to the hamburger menu button. To fix this I needed some way of changing the padding if this item, but alas, no easy way exists of doing this. Well, I say that, but after a little bit of work it turns out it’s not that hard after all.

The general idea of what I am about to describe can probably be used in other places to style android build in components where you aren’t given direct control.

So, without further ado, I will explain how to change the padding on the left action button.

First using the UIAutomator tool in the Android SDK we can find the resource ID of all the items on the context menu. So, once you’ve implemented the Contextual Action Mode, get it up on the screen and use the tool, as described in the above link to obtain the ID. In my case the ID is

The next step is to get an actual reference to this view. This is easy; if you are within an activity you can simply use


or, if you are within a fragment


From there, you can enact anything on the view that you normally would. As I said, I wished to change the padding so all in all my single line of code looked similar to this:

getActivity().findViewById(, 0, 50, 0);

So there you have it, now you can find and manipulate any view within Android that you can find!


Android MediaPlayer – A brief introduction

Recently my boss approached me to say that one of our clients wanted a custom made audio player app creating. This is something I have never touched on before, and my original thoughts on the matter were along the lines of “Oh sugar…”. But it turns out that Android has an incredibly easy to use MediaPlayer class. So the basics of what you need to know to get started:

To create an instance of the MediaPlayer class, ready to play a track you can use

MediaPlayer player = MediaPlayer.create(context, R.raw.sound_file_1);

This is assuming that you’re trying to load a file placed in your apps raw resources folder. The context just needs to be any valid context object. From this, to play the file all you need to do is call start(). A few of the most basic commands you might want to use include:

int currentPosition = player.getCurrentPosition();
player.seekTo(currentPosition + 1000);
player.seekTo(currentPosition - 1000);

getCurrentPosition() obtains the current position on the current track in milliseconds. seekTo() takes an argument in milliseconds of where to jump to in the track. Hence, seekTo(currentPosition + 1000) will jump 1 second forward (fast forward) in the track, and the opposite will jump back (rewind) 1 second.
player.stop() will stop the current track. If you call start() after stop() has been called you will get an error.

In a later post I will be showing a little more detail on how to use this class, and some ways to make managing media with it easier.


Word of the Week – Crepuscular

Crepuscular… According to the Oxford dictionary this means

Resembling or relating to twilight

I happened upon this word whilst reading one of my favourite authors, who happens to use quite a variety of interesting and rarely used words.