Creating a library for Android

I already went through the process of creating an application from the command line. This time I am going to show how to create a library project and integrate it into your app.

To create your library project create a folder and execute this command from inside that folder:

1
2
3
4
android create lib-project \
--name <project_name> \
--target 1 --path . \
--package com.example.whatever

Then you can you ahead and write some code. I created a view that will print a hello message (src/com/ncona/hello/Hello.java):

Read More

Array Pair Sum

This is the first post on a series where I will be resolving some common coding interview questions.

The question

Given an integer array, output all pairs that sum up to a specific value k.

My solution

I like to start by creating a test for my solution:

1
2
3
var arr = [1, 4, 2, 6, 8, 3, 9, 0, 7];
var res = findPairs(arr, 7);
// I expect res to be [[1, 6], [4, 3], [0, 7]]
Read More

Building back stack correctly when coming from a notification on Android

After I added some notifications to my app I noticed that they weren’t behaving the way I wanted them to behave. When I clicked on a notification I landed on one of my internal screens. When I clicked back I was expecting to go to the main screen but instead it went back to the phone home.

After reading a little I discovered that there is a difference between navigating back and navigating up (Read the related links to learn more), and that I needed to properly set the up navigation before I could fix this.

To set the up navigation correctly you need to specify a parent for each of your activities in your manifest:

1
2
3
4
5
6
7
<activity android:name="Internal" android:label="@string/app_name"
       android:screenOrientation="portrait"
       android:parentActivityName=".MainActivity">
    <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value=".MainActivity" />
</activity>
Read More

Schedule your Android app to do something periodically

I am writing a little Android app that will send alarms to the user based on certain rules. For this there are certain things that I need:

  • I want to have something that will run periodically and check my DB to see if there are any alarms I should send to the user.
  • I want this to run even if my app is closed or the phone is asleep
  • I want this to start automatically every time the phone is turned on

I did a little research and these are the pieces we need:

  • Services – To have our app do something in the background
  • AlarmManager – To schedule the service to be executed in the future
  • ACTION_BOOT_COMPLETED – This is a broadcast intent that Android sends when it boots

Now, lets start putting things together.

Read More

Setting up Google Analytics for Android

Setting up Google Analytics for a website is as simple as adding a snippet of JS on each of your pages. I was expecting the same for Android apps, but it seems like you need to follow a few steps to get this to work.

Setting up Google Play services SDK

The Google Analytics library for Android needs to communicate with Google Play API, for this reason, we need to install the SDK. Run this command:

1
android

And install the Google Play services package:

google_play_services

Read More

Android UI Automation using Espresso

I asked around my Android developers which was the best framework out there for Android automation and the most convincing answer was Espresso, so I decided to give it a try. Espresso is designed to be used in environments where the developers write their own tests (which I think should be everywhere), and promises a concise and beautiful syntax.

Set up

To use espresso you have to set up a test project. I decided I was going to place all my automation tests under tests/automation so I created that folder, moved into it and ran this command:

1
2
3
4
android create test-project \
-n MyProjectAutomation \
-p . \
-m ../../
Read More

Creating a grunt plugin

I have been using grunt for a while but some of my tasks started getting a little ugly because I started abusing grunt-shell. Because of this I decided to create a grunt plugin for something that I need for most of my JS projects, unit tests.

To run unit test I use VenusJS test runner for which there wasn’t a grunt plugin. So I decided to create one. Grunt documentation has some information on creating plugins but I felt there were some things missing.

This is the process I followed to create my plugin:

Install grunt-init by running:

1
npm install -g grunt-init
Read More

Make vim Command-T ignore certain files

Since I have been working with Java I found annoying every time I used Command-T to look for a file it showed me not only the source code file, but also the .class file. Looking at the documentation I found that there is a way to have Command-T ignore certain files. Just add this to your .vimrc

1
set wildignore+=*.class
Read More

Hash tables

Hash tables are a very important data structure that can be used for many things. They are really fast so they are a good fit for almost anything where they can be used. One example of when a hash table is useful is an associative array, for example:

1
2
3
4
5
var a = {};
a['hello'] = 'hola';
a['bye'] = 'adios';

console.log(a['hello']); // Prints hola

The beauty of hash tables is that searching for the value of a[‘hello’] (ideally)takes the same time no matter how many values the associative array has. In JavaScript we have this functionality with plain objects, but I needed to understand a little more about it’s implementation so I will explain how you could create your own hash table using arrays.

Read More

Configure syntastic to work fine with Android projects

Synstastic is a syntax checker for many programming languages, including Java. The problem that I was having is that for my Android project it wasn’t helping me at all because it couldn’t find any of the Android libraries and almost every line showed as an error.

The reason for this is that syntastic uses javac in the background to look at the file and find out if there are any errors. It does a good job for classes in the java standard library, but it doesn’t know where to find the Android SDK so it throws errors for every line that makes use of it. To fix this we need to add the java SDK to our path.

There are two ways of doing this. The first one will only last for the length of the vim session and will go away when you quit. Use this vim command:

1
:SyntasticJavacEditClasspath
Read More