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 Development – Preferences

  If you have ever developed an application for mobile, most chances are you have found yourself wanting to create a settings page. The user uses the settings page to set configuration parameters and these parameters are saved even after the program is closed. In this post I will explain how to create a settings page very easily. All code examples are available to download at the bottom. Android designers have developed a framework to make the developers life a lot easier. Preference Framework is a framework composed of a hierarchy of preference objects which are translated to UI objects. This mechanism is built on top of the shared preferences mechanism which stores the values. Shared Preferences Mechanism Before we can explain how to build a settings page with the preferences framework we need to get to know the shared preferences mechanism. Shared Preferences is a simple key-value storage mechanism of primitive types and Strings. These values are stored even if the program is terminated. Use it to store simple variables you wish to save between sessions of the program. Preferences Framework As mentioned, the preference framework is a framework which is built on top of the shared preferences mechanism. Continue reading Android Development – Preferences

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