Android UI’s für alle – MobileTechCon13 Video

Watch my Android multi screen support on JAXEnter

Adaptive Layouts werden von Android quasi seit jeher (1.6+) unterstützt. Ab Version 3.0 liegt mit den so genannten Fragments ein Konzept vor, das Smartphone- und Tabletentwicklung vereint. Die jüngste Qualitätsoffensive von Google, gezielt App-Entwickler für die Tabletentwicklung zu gewinnen, zeigt, dass es im Bereich der Cross-Device-UI-Entwicklung noch Informationsbedarf gibt. Der Talk von der MTC Spring liefert eine Einführung in die Konzepte der adaptiven UI-Gestaltung für Android. Ein Schlaglicht wird dabei auf die Umsetzungsmöglichkeiten von Tablet-UIs und deren Realisierung mit Fragments geworfen. Die Unterstützung durch das umfangreiche Tooling soll dabei nicht zu kurz kommen.

The slides:

Posted in talk | Tagged , , , , , | 1 Comment

Gradle, ActionBarSherlock and Android Studio

At this year’s Google I/O gradle was introduced as the new build system for Android.
Gradle makes perfect sense for automating Android builds on its own. But it makes even more sense with the tight integration into Android’s new default IDE – Android Studio aka Intellij IDEA.

The most important aspect of that is

A Single Build System – Gradle is the authoritative build across the IDE and the command-line. This means that the same build that powers Android Studio will power your command-line and continuous-integration builds.

Setting up your project with gradle is pretty simple. Just create a new project in Android Studio. I recommend creating a dummy project just for educational purpose to see how a typical gradle setup looks like. There are instructions how to port Eclipse projects to gradle / Android Studio too.

The standard Android gradle build script with no dependencies but the support library looks like the following:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Line 1-9 take care of the setup of the Android build specialities and will look the same in all Android build scripts. The rest is declaration of dependencies and build and target version. For detailed information see the documentation or watch the Google I/O video.

Integrating ActionBarSherlock

Update 2013-09-27:
with the support of aar library project bundles the integration became a no brainer:

dependencies {
    ...
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

ActionBarSherlock is integrated as a library project. The current master branch does not feature gradle support yet, so you have to use the dev branch:
https://github.com/JakeWharton/ActionBarSherlock/tree/dev

Naturally that branch is integrated as a git submodule:

git submodule add -b dev \ 

https://github.com/JakeWharton/ActionBarSherlock.git

which adds the following to .gitmodules:

submodule "ActionBarSherlock"]
        path = ActionBarSherlock
        url = https://github.com/JakeWharton/ActionBarSherlock.git
        branch = dev

and sets up the ActionBarSherlock submodule as a sibling to your project folder.

Create a top level gradle settings file – ./settings.gradle:

include ':ActionBarSherlock:actionbarsherlock'
include ':MyProject'

Optionally create a gradle settings file in your project to enable local builds there -
MyProject/settings.gradle:

// local include to enable build from '.'
include ':ActionBarSherlock:actionbarsherlock'
project(':ActionBarSherlock:actionbarsherlock').projectDir = new File('../ActionBarSherlock/actionbarsherlock')

Setup your project build script to add ActionBarSherlock as a dependency – MyProject/build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile project(':ActionBarSherlock:actionbarsherlock')
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 17
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

Note: the specific project setup is handled in the sourceSets section

Android Studio project import

To import the project into Android Studio, select File / Import Project …settings.gradle of the top level directory. Make sure your Android Studio SDK has Version 14 downloaded (required by ActionBarSherlock).

Caveat Emptor

Android Studio prior 0.2.0 and gradle plugin prior 0.5.+ prevented you from running / debugging your app from inside the IDE…
See https://code.google.com/p/android/issues/detail?id=57229 for details.

Posted in environment | Tagged , , , , , | 67 Comments

AsyncTask exception handling

Did you realize that AsyncTask is utterly broken with respect to exception handling?

Consider the following code where some Loader implementation downloads the content of a web page into a string:

private class PageLoadTask extends AsyncTask<URL, Void, String> {

     protected String doInBackground(URL... urls) {
         return new Loader().load(urls[0]);
     }

     protected void onPostExecute(String result) {
         // handle / show result in UI
     }
 }

What happens, if new Loader().load() throws an exception?
You get a stacktrace in your background thread.

But what if you want to react to such a possible exception?

You need to introduce a result holder


private class PageResultHolder {
    private String content;
    private Exception exception;
}

private class PageLoadTask extends AsyncTask<URL, Void, PageResultHolder> {

     protected PageResultHolder doInBackground(URL... urls) {
         PageResultHolder result = new PageResultHolder();
         try {
             result.content = new Loader().load(urls[0]);
         } catch (Exception ex) {
             result.exception = ex;
         }
         return result;
     }

     protected void onPostExecute(PageResultHolder result) {
         if (result.exception != null) {
             // handle error
         } else {
             // handle / show result
         }
     }
 }

Silly. Why isn’t there something like a onPostException(Exception ex) callback?

Posted in does and don'ts | Tagged , , | 1 Comment

Android UIs für alle(s) – slides

Had a great day at mobile tech con in Münich yesterday!

Thanks to all attendees of my presentation. The presentation is available on dropbox.

A short interview with Claudia Fröhling can be found here.

Posted in Uncategorized | Tagged , , , | 1 Comment

Responsive Android Design – Web Resources

I prepared a list of resources of responsive android design for the upcoming Mobile Tech Conference. So I thought it would be useful to share the collected information with the broader public.

Developing a responsive design on Android involves the following topics:

  • multi-resolution
  • various screen sizes and forms (form factors)
  • unified tablet and smartphone development (fragments)
  • tooling

Please drop a comment in case I missed a good resource.

Resources

Official Documentation

Friday App Clinic

Android Design in Action

Google I/O

Juhani Lehtimäki

Others

Posted in resources | Tagged , , , , , | 2 Comments

Reine Formsache, Mobile Technology 1/2013

My latest article is featured in the current issue of Mobile Technology:

“Reine Formsache – Formfaktoren bei der Androidentwicklung” – Andreas Hölzl

“form matters – form factor in Android development” – Andy Woodly :-)

The articles comprises all the basics you need to know to successfully support the full range of Android devices out in the wild ranging from normally sized smartphones, mini-tablet to the latest Nexus 10.

Just recently the tool support for Android made a major leap forward.

It’s all there, just use it (properly) and get to know the platform.

Posted in publication, ui | Tagged , , , | Leave a comment

Upcoming talk @mobiletechcon Munich, March 2013

My talk about successful cross-device Android UI development got accepted for the upcoming MobileTechCon 2013 in Munich!
Reserve the date:

MobileTechCon 2013, Munich

The talk focuses on 3 main topics:

  • multi-screen support
  • unified smartphone and tablet development using fragments
  • tooling support

Looking forward to meet you there!

Posted in event, talk | Tagged , , , , | 1 Comment

A better HashMap<Integer, E>, idle GC

Any (Android) developer sooner or later faces the need to keep some state in his / her application. Quite often a map (int -> object) achieves that.

In Java the natural choice would be a (Hash)Map (see trove for another option):

HashMap<Integer, E>

On Android this is suboptimal.
Generics involve objects and objects need to be garbage collected at some stage.

This contradicts one of the main performance rules on Android:

Keep the garbage collector as idle as possible!

So, a better solution is to use androids *Sparse* implementations in the android.util package which significantly reduce the number of involved objects:

https://developers.google.com/events/io/sessions/gooio2012/103/

(c) Google, https://developers.google.com/events/io/sessions/gooio2012/103/

Posted in does and don'ts, performance | Tagged , , | Leave a comment

Fragmentation Talk @JAX – April 19th, 2012

At this years JAX conference in Mainz, Andreas will give a talk about fragmentation on the Android platform.

Reserve the date:

JAX, Rheingoldhalle, Mainz – 19.04.2012 | 16:00 – 17:00

The talk covers all facets of fragmentation seen on the Android platform including the correct antidote to cure them.

The main fragmentation issues are:

  • Hardware features
  • API levels
  • Screen sizes / resolution
  • tablet vs. phone dev
  • issues with the support package (fragments backport)
Posted in event, talk | Tagged , , | 3 Comments

The (lack of) usability of the ninepatch tool

If you’re into Android development (which you are, since you’re reading this), you know the draw9patch tool which comes with the Android SDK.  For reference, a couple of links:
The 9patch concept
The draw9patch tool

In the last days I used it quite a lot and I’m really frustrated with its usability, or lack thereof. Here are a couple of things that could have been implemented better, to put it mildly:

  • When drawing the border I must click exactly on the 1-pixel border, which is hard, especially with larger images. I would like to be able to simply drag a selection rectangle around the border area which I want to paint black, and not go pixel-hunting.
  • Most images have some symmetry, and I want them to remain symmetric after resizing. For that I need again to go pixel-peeping, to make sure I have the same number of black pixels on both sides of the central axis. Some simple alignment tools would make this a lot easier.
  • When you edit buttons, you’ll have 2-3 images with the same dimension and minor variations, representing the states of the buttons. I want to be able to copy the 9patch markings from one image to the other, ensuring that they resize consistently. Right now you need to do it by pixel-counting.
  • Last but not least, it shouldn’t be so hard for the tool to remember the directory where I saved my last image, and open the Save dialog with that directory selected. It kinda sucks having to navigate from my home directory all the way to the project directory for every file.

So that’s my rant, I had to get it out. Now, since draw9patch is open-source, I’d better get down and code some of these improvements myself instead of bitching about them. Maybe we’ll even hold a Hackergarten for it!

Posted in rant | Tagged , , , | 3 Comments