Category Archives: Android

All posts related to Android programming

Applying Command pattern in a demo of undoable actions.

In programming, we are usually need to queue requests, or decouple implementation of requests from its caller. And this is reason Command pattern was given birth. The Command pattern, basically, allows us encapsulate commands into objects. Thereby, it provides some useful options, such as:

  • Queue or dequeue requests
  • Parameterize client with different requests
  • Support redo/undo functionality

The implementation of Command pattern is quite simple with four major terms: Receiver, Invoker, Command, ConcreteCommand and Client. Let take a look at bellow diagram: Continue reading

Adapter pattern

Sometimes existing classes already provide functionalities required by client, but its interface is not what the client expects. So that we need a bridge to convert existing interface to what the client expects. And the bridge is called an Adapter. In real world, an adapter is very popular such as adapters for supplies, adapters for memory cards, so on. In software programming, the features of adapter are provided by Adapter pattern. We are going into its details in this article.

Adapter pattern helps us with two main jobs:

  • Convert the interface of a class into another interface what client expects.
  • Adapter lets classes work together, that couldn’t be able because of incompatible interfaces.

Continue reading

URLConnection with Cookies

Updated on 25/11/2015: You should take a note about cookie with redirect-request. By default HttpURLConnection will take care about it automatically. You set your wide-system CookieManager, you pass a cookie to request, everything is OK.

But in case you have no default CookieManager and you do request without Cookie in header, you will get the problem. There’s no cookie was returned with response. This is because of HttpURLConnection set/get cookie for each request/response through the wide-system CookieManager instance. Let see bellow flow:

  1. Client sends a request without Cookie in header.
  2. Server returns a response which has code 301, new Localtion and Cookie inside header.
  3. HttpURLConnection looks at the header of response. It takes the Cookie then checks the global CookieManager instance. The instance if NULL then no cookie was saved.
  4. HttpURLConnection automatically make other request due to the Location. But because of no instance of CookieManager is available, then this request has no Cookie header too.
  5. Server receives the redirect request and sees no Cookie in header. It understands that no need to use Cookie mechanism. So it returns a request which has no Cookie.

To solve this problem, we can disable automatically process redirection by method setInstanceFollowRedirects(boolean) and do it by yourself. Notice that at step 3, you will extract Cookie header and set it to step 4 manually by setRequestProperty(String, String) and getHeaderField(String key).

As we know that Android 6.0 releases doesn’t support Apache HTTP client anymore. Instead of this, we will use the HttpURLConnection class. This API is more efficient, and in fact there are many interesting things to do with it. However today I only start with managing cookies. In that I will just focus on customize your own CookieStore. Continue reading

Take a look at the Java Reflection

Well, before taking in-deep look at reflection, please keep in mind that if it is imposible to use other method for operation, we should use this way rather than the reflection.

So, what is reflection? In the computer sience, reflection is the feature allows programs examine or modify runtime behaviour and structure of programs in the JVM. In Java language, reflection allows programs discover informations about classes, fields, interfaces and methods of loaded classes, and use them without any security restrictions.

Here is a quick example to you how Java reflection works :

reflection_sample_1 Continue reading

Build your own Annotation processor using Java6 – Part 1

After previous post we all understood basic Annotation’s terms, how to custom it, how to retrieve it at Runtime by reflection. To day, i’m going more deeply, such a writing Annotation processor, and integrate it with eclipse to reuse in a sample Android project.

Java SE5 provides a command-line utility for annotation processing, call Annotation processing tool or apt. It contains a set of reflective APIs and supporting infrastructure to process program annotations.

Since Java SE7, the apt tool and tis associated API have been deprecated, and are planned to be removed in next JDK release. Instead of it, use javac tool and APIs contained in the packages javax.annotation.processing and javax.lang.model to process annotations.

Continue reading

Exploring for Java Annotation

1. Introduction

Annotations, called metadata, is additional information that embed into java source code file, and it can be used by complilers or developement tools for some purposes :

Informations for compiler. Annotations can be used by compiler to detect errors or superess warning.

Compile-time and deployment-time processing. IDEs can use annatations to generate code, XML file, ….

Runtime processing. Some annotations ‘re available to retrieve at runtime. Continue reading

A smattering of focus on View

Before coming with focus, let’s go through 2 scenarios :

– There’re some EditTexts on screen, the first is focused. When user press NEXT button on soft keyboard, the pointer will move to next one with order from left to right, top to bottom. So what should we do when need to change the default focus movement order on Android?

– When user focus on an EditText, platfrom makes it hightlight and shows soft keyboard up. And the question is how do we customize action happen when one view get focus ?

In my two cents, these’re the most common necessary knowledge when we work with focus  on Android.

A view indicate its readiness to take focus through isFocusable() function. However we can change this attribute by call function setFocusable(boolean). Beside we also can specify view can take focus on touch mode or not through function setFocusableInTouchMode(boolean). After make view focusable, we can customize focus movement or action when focus happends. Continue reading

Working with WebView in Android

Basiclly, Webview is a View that displays web pages. It uses the Webkit rendering engine to display web pages, and includes methods to navigate forword, backword throught a history, zoom in and out, search texts, … It also provide mechanims that allows us interact with web content, and maybe receive event and sent command to javascript insdie page. Continue reading