Skip to main content

Android Studio: customize the “back to parent” ActionBar icon image (homeAsUpIndicator)

Like you probably already know, the ActionBar of an Android App can be set to allow backwards navigation from a child Activity to its parent, assuming that the relationship has been properly configured in the AndroidManifest.xml file:

We won’t go any further: for a full reference guide to Backwards / Up Navigation we strongly suggest reading the official documentation. We’re just pointing out that, in order to show the “up to parent” icon, we need to set up the  setDisplayHomeAsUpEnabled()  method accordingly:

This simple command will bring the backwards navigation icon into the leftmost side of the activity ActionBar

In order to replace the default theme icon with a custom drawable image we need to add the following declaration to our theme xml file:

The theme file location may vary: it can be  /values/styles.xml  or   /values/themes.xml  or even another file, depending on your app project configuration. Also remember that, if you are supporting pre-11 APIs, the above declaration should be put into the theme file located into the  /values-v11/ folder, whileast the theme file in  /values/  would require the old API style syntax instead:


Here’s a couple .rar archives containing some homeAsUpIndicator icons you can use to replace your theme‘s default ones: they are both white, so they could be a nice fit if your ActionBar features a black or dark-colored background color.

Happy coding!

Android: proportionally stretch an ImageView to fit the whole screen width while maintaining its aspect ratio

Proportional image resizing is a fairly common scenario while developing an Android app: there are a number of situations where you might want an image to stretch itself to horizontally fit the whole screen while keeping its original aspect ratio. You might think it should be easy: sadly, it’s not.

The problem

Achieving this result in HTML would be extremely easy: we just need to set our image width to 100% and avoid to set any height, letting the browser doing the whole proportional resize job. Unfortunately, Android wears a different pair of shoes: the ImageView element containing your drawable resource (or downloaded file) requires a width and an height: we can give a fixed number in px, dp or other supported units, use the parent width with the match_parent command or use the actual image width using the wrap_content command: as we can see, proportional resize is not an option. We can achieve some decent results by setting android:layout_widthandroid:layout_height and android:scaleType to make them adapt to the container layout in the following way:

These values could work in your specific scenario, but they tend to give inconsistant results among different devices and, most importantly, among different image sizes/ratios.

The solution

Why should we use a plain ImageView? We can start by taking a look to this interesting post on StackOverflow showing how to extend a View in order to achieve a very similar result: then we can use that knowledge to build our own ImageView extension class. Here’s an example of a ProportionalImageView which automatically scales its height according to its computed width keeping its original aspect ratio intact:


And this is how we can use it inside an XML layout:

Just remember to replace with your app namespace and you should be fine.

Happy coding!


Android SDK: when Eclipse hangs on “Resolving error markers” or “Android SDK Content Loader”

Any Android developers who chose to not adopt Android Studio and continue using Eclipse will eventually face the dreadful problem of the workspace not loading properly. The most common jobs who tend to get stuck are: Android SDK: Resolving error markers… and Android SDK Content Loader, which sometimes hangs at 0% or 100% of their completion and also tend to completely block the GUI when it’s the case.

The first thing to try when this happens is to run Eclipse with the -clean and -refresh command-line options enabled:

There is a fair chance that this simple method will solve the issue. Since it tends to happen a lot it would be advisable to put the trick in a batch file in the Eclipse root folder (i named mine eclipse.clean.refresh.bat):

There are, however, scenarios in which this method doesn’t work. When such situations arise we need to switch to a more aggressive approach, deleting the cache files built by the SDK in the .android folder which is located inside the current user profile files. Here’s the updated batch file to fullfill the task:

Before executing any of these commands ensure that the Eclipse process is properly closed, otherwise the files and the workspace will be locked: if you cannot do that gracefully, use the “Kill Process” option in Task Manager instead.