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

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

Android – Opening a new screen

  Opening a new screen is fairly easy in android. This post will show you almost all you need to know about switching back and forth between screens in your Android application. Add all activities to manifest Each screen is usually an Activity. Each activity should be mentioned in the manifest file. The simple use Bear in mind that every time you will call the function startActivity(intent) a new instance of MyActivity2 will be created. Passing a parameter In most times you need to pass a parameter to the new screen. You do that by using the function putExtra(). This function Receiving a parameter And of course you will need to receive that parameter on the other side. Going back If you need to go back to the previous screen and terminate the current screen, than all you have to do is to use the function finish() from anywhere in the new Activity. Open for result When you need to open a new screen and get result back from it, than use the function startActivityForResult() instead of startActivity() Returning a result Getting the result from the new activity