Wednesday, 16 April 2014

Install Jenkins on Ubuntu

Jenkins is a continuos integration environment which running on a server, it could connect to a repository such as Git or SVN to retrieve the last version of code and compile it following some general rules.

It is recommended for big projects to ensure that for any new change, the project will be compilable.

To install it on Ubuntu, just follow these steps:
1. Open the console or the terminal
2. Type the follow command to add the key

wget -q -O - | sudo apt-key add -

3. Type the follow command to add the repository of Jenkins into the list of repositories of your computer

sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list

It might ask you for your credentials. If so, type it and continue.

4. Type the follow command to update the list of resources

sudo apt-get update

5. Type the follow command to install jenkins

sudo apt-get install jenkins

Source and more information:

Friday, 4 April 2014

Setting multiusers for GenyMotion

GenyMotion is a tool for Android which runs the emulators on the Virtual Box of Oracle. It has plugins for eclipse, which allow the user to download and start a emulator faster than the native ones.

Since Android 4.2, the tablet version of Android has multiple users functions. This allows different owners of the same tablet have different spaces, background, accounts, etc. It is something like the multiple users function that Windows, Linux and Mac OS has.

By default, the multiple users function for the emulators of GenyMotion has been disabled. But it could be enabled with the follow commands:

1. Connect to the emulator
1.1. Starts the emulator of GenyMotion. It must has a version older than 4.2 and it must be a tablet (i.e. Nexus 10)
1.2 Open a terminal
1.3 Type the follow command in your terminal to connect to the emulator

adb -e shell

2. Check the maximum support number of users allowed
2.1 After the connection has been established, type the follow:

pm get-max-users
You will get something like:

Maximum supported users: 1

This is the default number of maximum support users.

3. Change the maximum support number of users allowed
3.1 On the terminal type the follow command to remount the disk:

mount -o rw,remount /system

3.2 Change the value in the system file typing the follow command in the terminal:

echo fw.max_users=4>> /system/build.prop

4. Restart the emulator
The easiest way to restart the emulator is use the mouse and do long click on the turn off button on the right bottom corner and select power off option.

After it, close the emulator and start it again from the plugin of Eclipse.

5. Check it
Now if you go to the setting, you will see a option called "Users" below the "Apps" option.


Friday, 3 January 2014

My singleton for Android

From Wikipedia, there are many way to creates an singleton in Java. The simplest and clearest version is the follow one:

public class SingletonDemo {
    private static SingletonDemo instance = null;

    private SingletonDemo() { }

    public static synchronized SingletonDemo getInstance() {
        if (instance == null) {
            instance = new SingletonDemo ();
return instance;

But in most of the cases, it is not suitable for Android because the constructor requires the context. So, this is the singleton that I use:

public class SingletonDemo {
    private static SingletonDemo instance = null;
    private static Context context;

     * To initialize the class. It must be called before call the method getInstance()
     * @param ctx The Context used

    public static void initialize(Context ctx) {
    context = ctx;

     * Check if the class has been initialized
     * @return true  if the class has been initialized
     *         false Otherwise
    public static boolean hasBeenInitialized() {
    return context != null;


    * The private constructor. Here you can use the context to initialize your variables.
    private SingletonDemo() {
        // Use context to initialize the variables.

    * The main method used to get the instance
    public static synchronized SingletonDemo getInstance() {
    if (context == null) {
    throw new IllegalArgumentException("Impossible to get the instance. This class must be initialized before");

    if (instance == null) {
    instance = new SingletonDemo();

    return instance;

    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException("Clone is not allowed.");


Note that the method initialize could be called in the main class and the method getInstance could be called from other classes. This will fix the problem when the caller class requires the singleton but it does not have the context.

Finally the method hasBeenInitialized is uses to check if the class has been initialized. This will avoid that different instances have different contexts.

Tuesday, 31 December 2013

AutoCompleteTextView - A simple hack to work with complex data models

AutoCompleteTextView was added from the API Level 1 and it is very useful to show a list of hints while the user is typing.

Checking the source code, I found an easy way to hack the view to show the hints for the complex data models. Here is an example:

1. Model:
The model contains 3 fileds: id, name and complete address. The hack overrides the method toString to return the name.

 * Hack. Do not modify. To String Must return the name of the cinema, which is the one returned
 * by the AutoCompleteTextView.
public String toString() {
    return getCinemaName();

2. Main activity
For the main activity, a simple ArrayAdapter is used:
ArrayAdapter<Cinema> adapter = new ArrayAdapter<Cinema>(this,
                android.R.layout.simple_dropdown_item_1line, cinemas);

This is the result:

Checking the source code of AutoCompleteTextView you can see that it uses the a list adapter to show the list of hints. By using the ArrayAdapter, which in the method getView it uses the method toString to set the text shown:

T item = getItem(position);
 if (item instanceof CharSequence) {
 } else {

So, by overriding the method toString to output the string to be shown as hint, it could work.

Source Code
GitHub - AutoCompleteTextViewHack

More information
Android developers - AutoCompleteTextView

Both AutoCompleteTextView and android.R.layout.simple_dropdown_item_1line has problem with old devices. I have tested it on a Samsung Galaxy Ace (GT-S5830) with 2.3.5 and both views shows a white background and white text colour, making the text impossible to see.

For the AutoCompleteTextView, the fix is easy:
        <requestFocus />

And for the dropdown item, the source code could be found in the internet, which the app could have a local version with modification of colours:
<TextView xmlns:android="" 

Monday, 30 December 2013

Customize the home up button in the Action Bar (Sherlock)

Like any other elements of Sherlock Action Bar, the home up button, which is the small arrow on the left side of the home(logo) button, could be customized.

Here is a short guide for it:
1. Override the actual style for the pre-v11 devices.
1.1 Copy and paste the follow code in the file styles.xml of the default values folder.
    <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
    <item name="homeAsUpIndicator">@drawable/ic_home_up</item>

Note that the parent could be changed to any Sherlock theme.

2. Override the actual style for the v11+ devices.
2.1 On the same folder where the folder values is, create a new folder called values-v11. Android will automatically look for the content of this folder for devices with API or above.
2.2 Create a new file called styles.xml
2.3 Paste the follow code into the file:
<?xml version="1.0" encoding="utf-8"?>

    <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
    <item name="android:homeAsUpIndicator">@drawable/ic_home_up</item>

Note tha the name of the style must be the same as the file in the default values folder and instead of the item homeAsUpIndicator, it is called android:homeAsUpIndicator.

The item issue is because for devices with API 11 or above, Sherlock Action Bar use the default Action Bar which comes with Android, which the key name is android:homeAsUpIndicator. But for the devices with API 10 or lower, Sherlock Action Bar uses its own ActionBar, which the home as up indicator is called simple "homeAsUpIndicator".

3. Use the new theme in the manifest
3.1 Replace the theme for the application/activity in the AndroidManifest file:
    android:theme="@style/MyCustomTheme" >

Source Code:



Enable the Indeterminate progress bar in the Sherlock Action Bar

The Sherlock action bar comes itself with an indeterminate progress bar. To activate it, a new flag must be enabled, which must be before the content has been set:

protected void onCreate(Bundle savedInstanceState) {

    // Request the progress bar


Once this flag is enabled, to enable the progress bar just do the follow:


And to disable it:

Be aware there when the flag is set, there are two imports possible. The one which is native is the good one:

import android.view.Window;



Thursday, 21 November 2013

Installing the Subclipse to use Subversion on Eclipse

Eclipse has a very popular plugin to integrate the subversion on Eclipse. It is called subclipse and this is the steps to install it:

1. Install Subclipse
1.1 Open eclipse
1.2 Clicks on Help -> Install new software
1.3 On the right site of "Work with:", type the follow:

1.4 Check all the software and click on next
1.5 Click on Ok when eclipse ask for security issues
1.6 Restarts the eclipse

2. Modify the configuration (Optional)
If you see any error about the library of JavaHL, you could fix it changing the configuration.
2.1 Open Prefences
2.2 Under Team -> SVN, change the "SVN interface" to

"SVNKit (Pure Java) SVNKit v1.3.5.7406"
or other versions.