Building Android UIs with Custom Views

After few more months working at nights, while commuting and weekends, I managed to write a book specialised on Android Custom Views.
It covers several topics: first it explains the reasons why we need to build custom views and what are the benefits and drawbacks of using them and then, describes how to start drawing our first shapes, add animations and user interactions and do some more complex rendering or create 3D custom Views in OpenGL ES. Finally, it also shows how to share and publish our custom View and how to optimize it for performance.
It’s available as ebook and printed format and, if you’re interested, you can grab a copy in amazon:

or alternatively:

This is a rather advanced book and requires some basic knowledge of how Android works. If you want to learn how to develop applications for Android I recommend to start with my previous book:

Learning Android Application Development

MWC Shanghai 2017

I know it has been a long time since the last post, but since last summer I was invited to talk at Mobile World Congress Shanghai and I still haven’t talked about the experience I thought it was about time to do so.
I spoke at the Transforming Industries Summit about the transformation we’re doing in order to retain and attract talent by increasing our exposure and letting the world know what are we doing.

Here is the video of the talk:

and the slides:

On a last minute notice, I also was designated the chairman of the summit, so with almost no time to prepare and with the help of all the other speakers at the event, I built a short script linking each talk with the following one.

At the end, it was a bit out of my comfort zone and I couldn’t prepare it as much as I wanted, but was a great experience.

More information about the Transforming Industries Summit:

After the event I took some time to enjoy the night skyline from Shanghai seen from The Bund:

Learning Android Application Development

These last months I’ve been very busy writing a book about Android development. It covers from the very basics to application testing and using third party libraries to make your life as a developer easier.
It’s available both in ebook and printed format, if you’re interested, grab a copy!

III Jornades Emprenedoria i Tècniques Institut Poblenou

Few weeks ago I was invited as a speaker to the “Jornades Emprenedoria i Tècniques Institut Poblenou” and yesterday I went there and gave a talk about product vs project, lean approach, ux and, at the end I explained the AngelHack experience, what are hackathons, why is useful to attend, …
There was a good attendance (~80-90), room was packed and there were people standing at the end of the room. I really enjoyed being able to talk to that audience, to introduce them to the world of hackathons and giving them feedback about their projects and ideas.
Here are the slides (sorry, only available in catalan)
And some pictures of the event:

Speaking at mobile development conferences

These last months I’ve been speaking in some mobile conferences:
Mobiconf (Krakow – Poland)
Droidcon UK (London)
Droidcon NL (Amsterdam)
Here is a video recording of my presentation at Droidcon UK: (you can create a free account with skills matter if you don’t have one)
And here is a link to the slides, with a nice summary made by the Droidcon NL organisers:
Big thanks to Eric Lafortune for the mention in their blog!

BcnDevCon Presentation – Improving Java & Dalvik Code Performance

Last week I did a presentation at BcnDevCon about improving Java Code Performance. The focus of the presentation was showing some examples of compiled java sources and evaluate the performance impact of different ways of looping, string concatenation or using Java 1.5 features as autoboxing or foreach loops. According to java the performance optimizations are always left to the JVM, but we will see we can do many things to improve our code performance by knowing how the compiler works.
Some of these examples are also shown on Davlvik bytecode and performance tests are executed on both a computer and an android device. Even if Dalvik is register based and standard java bytecode is stack base in general terms what works for standard java can be also applied for Android apps.
On future posts I will explain in more detail the performance graphs and other topics I didn’t had time to include in the presentation.
Screen Shot 2013-11-15 at 00.51.53

What your mom didn't tell you about autoboxing…

Autoboxing is a nice feature added to Java 5 to avoid writing boxing code to add, for example, primitive data types to a Collection as you can only add the appropriate wrapper class (Integer for int, Long for long, …). I’ve recently seen the autoboxing feature being widely used all over the place so I decided to write this post.
Oracle itself warns that “It is plenty fast enough for occasional use, but it would be folly to use it in a performance critical inner loop.” and “An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.“. So that basically means do not use autoboxing on portions of code where performance counts.
To illustrate the problem I’ve written an example:

public class Autoboxing {
// method using "accidentally" autoboxing
public void method1() {
long t = System.currentTimeMillis();
Long total = 0l;
for(Integer i = 0; i < 10000000; i++) {
total += i;
System.out.println(" total method1: " + total + " time:" + (System.currentTimeMillis() - t));
// method not using autoboxing
public void method2() {
long t = System.currentTimeMillis();
long total = 0;
for(int i = 0; i < 10000000; i++) {
total += i;
System.out.println(" total method2: " + total + " time:" + (System.currentTimeMillis() - t));
public static void main(String[] args) {
Autoboxing abox = new Autoboxing();

The first method method1 uses some autoboxing (the Integer and the Long). The second method method2 only uses primitive data types. Both methods iterate 1.000.000 times and when they finish they print the total and the amount of milliseconds since they started.
The bytecode of the for loop of both methods already indicates, subtly, that method2 will be more efficient:
Bytecode for loop method1:

9: iconst_0
10: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
13: astore 4
15: aload 4
17: invokevirtual #5; //Method java/lang/Integer.intValue:()I
20: ldc #6; //int 10000000
22: if_icmpge 65
25: aload_3
26: invokevirtual #7; //Method java/lang/Long.longValue:()J
29: aload 4
31: invokevirtual #5; //Method java/lang/Integer.intValue:()I
34: i2l
35: ladd
36: invokestatic #3; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
39: astore_3
40: aload 4
42: astore 5
44: aload 4
46: invokevirtual #5; //Method java/lang/Integer.intValue:()I
49: iconst_1
50: iadd
51: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
54: dup
55: astore 4
57: astore 6
59: aload 5
61: pop
62: goto 15

Bytecode for loop method2:

4: lconst_0
5: lstore_3
6: iconst_0
7: istore 5
9: iload 5
11: ldc #6; //int 10000000
13: if_icmpge 28
16: lload_3
17: iload 5
19: i2l
20: ladd
21: lstore_3
22: iinc 5, 1
25: goto 9

In terms of timing, if we execute this class we get the following result:

rrafols$ java Autoboxing
total method1: 49999995000000 time:110
total method2: 49999995000000 time:9

So method2 takes only 8.18% of the total time of method1. Pretty big difference huh?
What happens if we run the same without enabling the JIT (Just in Time compiler)? Most mobile devices have very limited or non-existent JIT.

rrafols$ java -Xint Autoboxing
total method1: 49999995000000 time:7387
total method2: 49999995000000 time:141

Method2 takes only 1.91% of the total time of method1. Not only that, imagine we’re processing something that we have to show back to the user, there is a huge impact between having the user to wait for ~8 seconds or to wait for 150ms.
Please use common sense and be extra careful when using code you are actually not controlling or the compiler is generating for you.

Outcome BlackBerry 10 presentation

Last thursday I did an introduction to BlackBerry10 for Android developers. I focused most on the current limitations and possibilities of the Android Runtime of BlackBerry10 and I’ve also used some slides explaining how to convert a binary .apk file to a BlackBerry10 .bar file and sign using command line tools. There are, of course, easier tools (eclipse plugin, online repackager) but those don’t really need an explanation. At the end there is also a very quick introduction to QML/Qt and native vs cascades development.
Something we learned about the event is people is really tired after 4 days of Mobile World Congress and gets a bit lazy if it’s raining pretty heavily. Luckily the event was live-streamed and there is a recording of the whole presentation.
Find below some links of the event:
Pictures of the event at Google+
Slides (.pdf)
Video capture of the whole presentation
Please stay tuned for the next BlackBerry Group meeting in early April.

Presentation about BlackBerry 10

Next week during Mobile World Congress I am going to do a presentation about BlackBerry 10 together with Jorge del Casar, BlackBerry Developer Evangelist in Spain. The event is organized by GDG Barcelona and will be held in the Facultat Informàtica Barcelona
If you’re a developer it’s a good opportunity to get to know BlackBerry 10 as we will speak about both html5 and native development.
Here is the link to the event:
More information:

Few Android applications I've been working on…

I live in a rather small and calm city called Vilanova i la Geltrú, close to Barcelona and to other touristic places like Sitges, most of the time goes completely unnoticed. What applies to tourists also applies to some terms like technology, companies, … As an example, few months ago there wasn’t that much movement about mobile technologies but now seems that, slowly, companies and individuals are talking more and more about mobile apps, mobile web, mobile marketing, … As I already said, some times people doesn’t know what they need until you show them what are the options or opportunities of this “new” technology and then a new need is created for these people.
This is, of course, a very slow process. Last time I checked we weren’t yet at Silicon Valley. In Barcelona there are lot of companies, business, technological groups and, in general, global awareness about mobile technologies. Thanks in part because of the Mobile World Congress and the Mobile Wold Capital. I started noticing graphical designers are getting some small (or not that small) mobile projects, web development companies are starting to think how to provide mobile web solutions, and even some mobile app development companies are sprouting (some of them quite promising and some others not really).
My small contribution to start creating this conscience of mobile apps and mobile technology has been creating, in my free time, some small Android applications for my hometown. Give them a try, they’re a bit different (in UX/UI than most of the standard Android apps). Here is the list:
– RSS Reader of ‘Diari de Vilanova’ (Newspaper)

QR Code:
– Fira Novembre (November Fair of Vilanova i la Geltrú)

QR Code:
– Festa Major (Local Festivities of Vilanova i la Geltrú)

QR Code: