Android development – Custom Animation

Android developers web site provide you with 2 predefined animation techniques which you can use in your applications – Tween animation and frame animation. They are super easy to implement and work quite nice. If you are using Android 3.0 or above, you should check out Property Animation technicqe. But what if you need to create your own customized animations and you are using older versions? Don’t worry, that is easy to do as well, here is how you do it. To create a customized animation you need to follow these 3 steps. For the explanation, we will create a simple animation that changes the background of a button gradually from black to red. Step 1 – Extend the Animation class and set the properties Create a class which extends Android Animation class. This class will hold the logic of your animation. See my example: As you see, there is not much in this class since my animation is not that complicated. Notice that I have made all the necessary animation parameters initialization from inside the constructor, but you can defiantly initialize them from outside the class. There are 2 important parameters which determines the behavior of the animation: RepeatCount Continue reading Android development – Custom Animation

Android quick tip: use System.arraycopy()

Well it’s a well known fact that the java native function System.arraycopy() is a useful way to copy one array to another since it is native, but is that also the case for Android? And if so, how much more useful is it? To answer these questions I have made a simple test and ran it as a java program on my PC and than as an Android activity. Here is the test for the PC: Here are the results while running it from my PC (java 7, 8GB memory, CPU intel i5): Naive algorithm – 14 ms System.arraycopy(); – 6 ms. Arraycopy does the task in less than half of the time. Now to use it on Android – here is the code: * Notice I have reduced the size of the Array from 10 million to 1 million, this is due to restrictions on memory for applications in Android. The results from running it on my device (nexus 1): Naive algorithm – 182 ms System.arraycopy(); – 12 ms. This means that the fact that System.arraycopy() is better than the regular copy is even more true for Android. In short than, always use System.arraycopy() especially on Android. Test for Continue reading Android quick tip: use System.arraycopy()

Android – Creating links using linkify

  Linkify is a class that lets you create links from a TextView or a Spannable. You can create links not just to web pages, but also to locations on the map, emails and even phone numbers. *Note: the examples bellow may not always work in the emulator. Web address: Phone number: Map address: Email address: Auto detect: Use Linkify.ALL to automatically detect the link type. More than one option: You can choose more than a single option for the link type. Using pattern: You can use a regular expression for detecting text parts and transform only them to links instead of the whole text. MatchFilter: MatchFilter is used for more complicated filters. TransformFilter: So fat the text we have filtered stayed the same. But sometimes you need the text to be different than the text which is appended to the link. Download code example

Android – Multithreading in a UI environment

Why do we need multithreading in Android applications? Let’s say you want to do a very long operation when the user pushes a button. If you are not using another thread, it will look something like this: What will happen? The UI freezes. This is a really bad UI experience. The program may even crash. The problem in using threads in a UI environment So what will happen if we use a Thread for a long running operation. Let’s try a simple example: The result in this case is that the application crashes. 12-07 16:24:29.089: ERROR/AndroidRuntime(315): FATAL EXCEPTION: Thread-8 12-07 16:24:29.089: ERROR/AndroidRuntime(315): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 12-07 16:24:29.089: ERROR/AndroidRuntime(315): at … Clearly the Android OS wont let threads other than the main thread change UI elements. But why? Android UI toolkit, like many other UI environments, is not thread-safe. The solution A queue of messages. Each message is a job to be handled. Threads can add messages. Only a single thread pulls messages one by one from the queue. The same solution was implemented in swing (Event dispatching thread and SwingUtilities.invokeLater() ) Handler The Handler is the middleman between a Continue reading Android – Multithreading in a UI environment