HtmlUnit – A quick introduction

HtmlUnit is an open source java library for creating HTTP calls which imitate the browser functionality. HtmlUnit is mostly used for integration testing upon Unit test frameworks such as JUnit or TestNG. This is done by requesting web pages and asserting the results. Simple Example WebClient As you can see in the example, the WebClient is the starting point. It is the browser simulator. WebClient.getPage() is just like typing an address in the browser. It returns an HtmlPage object. HtmlPage HtmlPage represents a single web page along with all of it’s client’s data (HTML, JavaScript, CSS …). The HtmlPage lets you access to many of a web page content: Page source You can receive the page source as text or as XML. HTML Elements HtmlPage lets you ability to access any of the page HTML elements and all of their attributes and sub elements. This includes tables, images, input fields, divs or any other Html element you may imagine. Use the function getHtmlElementById() to get any of the page elements. Anchors Anchor is the representation of the Html tag <a href=”…” >link</a>. Use the functions getAnchorByName(), getAnchorByHref() and getAnchorByText() to easily access any of the anchors in the page. Dom Continue reading HtmlUnit – A quick introduction

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