Solving advanced animation problems

SDK Version: 

As we have seen in the previous animations tutorial it is easy to create some funny animations defined by XML.

Unfortunately in some cases we face troublesome limitations. Lest see them through an example:
We will create a simple animation that moves a buttom from the top left corner of the screen to the center when it is clicked, then remains there, and when clicked again it moves back to its original position

After you read the documentation it may seem easy to solve. We can define the animation realative to the parents size in percent (50%p), and the fillAfter attribute makes the button remain there, insted of jumping back to its original position. We can even define all of this in XML. So most of the problem seems to be solved with the few line of XML below:


Android animation basics

SDK Version: 

In android there are 4 basic types of animations:

  • AlphaAnimation
  • RotateAnimation
  • ScaleAnimation
  • TranslateAnimation

This animations can be highly customized and combined, for example we can set the speed, delay, acceleration, duration of animations, then group them together in an AnimationSet.

Animations can be applyed as Layout animations to ViewGroups, to be triggered when the ViewGroup is created/displayed or applied to any view and be triggered by us any time.

The animations can be definex in XMl or by code. Defining in xml is mutch more clear, but you can not set animation parameters dinamically.

The animation XML files should be created in the res/anim folder and can be accessed using the R class, like R.anim.animation_name.

An animation defined in hyperspace_jump.xml can look like this:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="">

How to draw multiline text to canvas easily

SDK Version: 

There are situations where you have to use a Canvas. What do you do if you have more text than you can properly display?
The problem is, that if you want multiline text on canvas, with the drawText method, you would have to measure how much space a single line of text would take up, and also compare it to the width of the screen, and draw each line separately.
Read more to check my solution.

Android image processing (detecting motions)

SDK Version: 

In this tutorial I'm going to show you how to process an image and draw it's color histogram.

First you have to create a new Android project with the following parameters.
Project name: ViewfinderEE368
Check the box for Android 2.0.1
Application name: Viewfinder EE368
Package name: com.example.viewfinderee368
Check the box for Create Activity and enter: ViewfinderEE368
Min SDK Version: 6

Advanced 9Patch drawing in android

SDK Version: 

Drawing 9patch can turn into a nightmare easily. I would like to present the other side. The most common problem, to create draws wich are look well in portrait mode and landscape mode too.

Here are some 9patch technics:

(Note: This article don’t tell the basics of 9patching. If you are new in this technics, examine this page first:

(From this link, you can download the files: download the 9patch files)

Example One: Simple button
This is a just simple button, with a glowing effect. The 9patching wich I use here, keeps the button’s edges, so it keeps the rounded rectangle’s radius too. With 9patch, the content area of the button can be set easily.


Flickr api, random image goodness

SDK Version: 

Lately we have been doing all kinds of demo projects, and tests, where we did not want to use constant data to present our products. Having a demo of your application is only nice, if it has relevant data in it. It could be top class technology, but if it is using the same image 20 times in a list, well that does not look very good.
Getting 20 relevant images on a subject takes up a lot of time, even with search engines and royalty free photo sites.Luckily most search able sites have free apis, here is a little snippet for flickr's services.

Wallpaper tutorial - Part 2

SDK Version: 

This article was requested by some of our community members. It is based on the Android Gallery, ImageView Example tutorial by Sasikumar (Part 1 is here). I extended his example with a new function. If you long click on the big image, an alert dialog show up and ask “Do you want to use this image as a wallpaper?". If you click yes, the actually image will be your new wallpaper.

I only changed the Activity class:

  1. package;
  3. import;
  5. import;
  6. import;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.res.TypedArray;
  10. import;

How to update custom listview images simply

SDK Version: 
So in this tutorial I'm going to show you how to refresh imageviews' contents periodically (let say by Handlers if you download the picture from web).
I had painful 2 days figuring out what's a good solution here, I tried to give IDs to imageviews and that sort of sick things, but believe me, it wasn't worth it. The solution is so simply that I hardly can believe.
What we're lookign for here is instead of create new Adapters and HashMaps (which contains ListView data), we just update it's values, and Android will do the trick for us.
The most important thing is DO NOT AT ANY CIRCUMSTANCES CREATE A NEW ADAPTER (or a new data source that holds the Adapter's data).
There's an exception of course, you obviously have to create a new Adapter in OnCreate() { }.

Let me show you how it works:

How to set an image as wallpaper on different api levels

SDK Version: 

Pretty easy! :)
Use the context’s setWallpaper method.

  1. getApplicationContext().setWallpaper(bitmap)

Don’t forget to add the SET_WALLPAPER permission to your AndroidManifest.xml!

  1. <uses-permission android:name="android.permission.SET_WALLPAPER" />

Differences between api versions
On Android 1.5 the wallpaper looks cool, doesn’t it? :) The backgroud fills the screen.

But on 1.6 and higher android strechs the wallpaper like this:

It’s not bad if you use high resolution wallpaper, but the low res wallpapers look crappy.

Syndicate content