Gradient background in iOS without images

One way to set a gradient background to a UI component is to create an image with the gradient colors and then assign it as the background image of the component. The alternative presented in this post is the use of the class CAGradientLayer.

A CAGradientLayer object is defined by these properties:

  • colors. An array of CGColorRef objects defining the color of each gradient stop.
  • locations. An optional array of NSNumber objects defining the location of each gradient stop.
  • startPoint. The start point of the gradient when drawn in the layer’s coordinate space. Default value is (0.5, 0.0).
  • endPoint. The end point of the gradient when drawn in the layer’s coordinate space. Default value is (0.5, 1.0).
 The first step is to import the QuartzCore package into your class. Do this by selecting your project target, then selecting “Build Phases” and “Link Binary With Libraries”. Press the add button and search for QuartzCore.

Add QuartzCore

Add this line to your import section.

#import <QuartzCore/QuartzCore.h>

In this example we are setting the gradient as the background of our view. Add the next code into the viewDidLoad method of the view.

// Create the colors
UIColor *darkOp =
 [UIColor colorWithRed:0.62f green:0.4f blue:0.42f alpha:1.0];
UIColor *lightOp =
 [UIColor colorWithRed:0.43f green:0.76f blue:0.07f alpha:1.0];

// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];

// Set colors
gradient.colors = [NSArray arrayWithObjects:

// Set bounds
gradient.frame = self.view.bounds;

// Add the gradient to the view
[self.view.layer insertSublayer:gradient atIndex:0];

Gradient iOS A

Try to change the default locations with this code:

gradient.locations = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:0.0f],
    [NSNumber numberWithFloat:0.2],

Gradient iOS B

Read More

Load a POST request into a WebView in Android

The standard way to load an URL into a WebView object is using the loadUrl method.

public void loadUrl (String url)

But if we need to load an URL into the WebView submitting POST data, use instead the postUrl method.

public void postUrl (String url, byte[] postData)

To create the POST data, use a string and encode it.

String postData = "submit=1&id=236";

    EncodingUtils.getBytes(postData, "BASE64"));

Read More

Introduction to software vulnerabilites

According to MITRE’s CVE Terminology, a vulnerability is a mistake in software that can be directly used by a hacker to gain access to a system or network, a state in a computing system which either:

  • Allows an attacker to execute commands as another user.
  • Allows an attacker to access data that is contrary to the specified access restrictions for that data.
  • Allows an attacker to pose as another entity.
  • Allows an attacker to conduct a denial of service.

Life cycle

The vulnerability life cycle consists of four events:

  • Discovery date. Somebody discovers that the vulnerability exists.
  • Disclosure date. A trusted security organization or the vulnerable software producer discloses the vulnerability existence.
  • Exploit date. Somebody creates an attack which exploits the vulnerability.
  • Patch date. Vulnerable software producer provides a solution or patch to protect the system.

It is not necessary that these four events occur in the vulnerability life cycle. A vulnerability can be exploited with no previous disclosure or can be patched with no previous exploit.

Vulnerability databases

Several databases store relevant data about vulnerabilities. Some examples are:

Read More

Droidcon Spain 2012

Yesterday I attended the I International Conference “Technological Innovation in a Mobile World” 2012. The conference was held in the Faculty of Economics and Business of the University of Murcia, Spain, and took place jointly with Droidcon, the international conference which deals with the technical and economic aspects of the mobile-devices platform Android.

Droidcon Murcia poster
Opening conference


More pics here.

Droidcon Spain Official Site

Some conference slides

Read More

Rounded button with shadow in Android

To create a rounded button, we need to create a new shape resource and configure it as shown below. This shape resource has to be assigned to the button as background, and can not only be applied to a button, but also to any component, like text fields, list elements, etc.

Rounded corners

Create the resource file in /res/drawable with “shape” as root element and include the element “corners”. This element has the attribute “radius” to set the value of the radius for all corners. To set different values for different corners, use the attributes “bottomRightRadius”, “bottomLeftRadius”, “topRightRadius” and “topLeftRadius”.

<shape android:shape="rectangle"

   <corners android:radius="20dip" />
     android:color="@color/drk_button" />

The gradient element is a gradient background and the stroke element configures the border of the button.


The next step is to create the shadow. The effect of the shadow can be done by adding a second shape under the original button shape, changing its color to a darker one.

<shape android:shape="rectangle"

     <solid android:color="@color/black_alpha"/>
     <corners android:radius="20dip"/>


To overlap different items we have to use a “layer-list” resource and include the previous shapes. The shadow should appear in first place and then the original button with some displacement. In this example, we are creating the shadow at the bottom of the button and an offset of 4px.


   <item android:drawable="@drawable/shadow"/>
     android:bottom="4px" />  


Compare results

Compare buttons

Read More