Running Android on your PC

I’ve recently discovered a neat way of running Android on my PC, thanks to some cool (and very very smart) developers: http://www.android-x86.org/getsourcecode

I discovered the website while I was trying to find a way of running Honeycomb (Android 3.0) , since the emulator is slow as hell even on my quad-core machine.

So, follow the tutorial, make a Live CD Android (I’m using a USB drive) and enjoy! It runs smooooooth! :)

Easy JSON parsing in Android

Here’s a very easy and fast way of parsing JSON I’ve been using the past few months: Jackson. It’s not Android-based, so you can use it on server or desktop applications as well, but I found it very useful on mobile because of the large amounts of data that resides in the cloud lately.

Enough with the chit-chat, here is an example. Let’s take this simple json string:

{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}

And this very simple Java class:

public class User {
    public enum Gender { MALE, FEMALE };

    public static class Name {
      private String _first, _last;

      public String getFirst() { return _first; }
      public String getLast() { return _last; }

      public void setFirst(String s) { _first = s; }
      public void setLast(String s) { _last = s; }
    }

    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;

    public Name getName() { return _name; }
    public boolean isVerified() { return _isVerified; }
    public Gender getGender() { return _gender; }
    public byte[] getUserImage() { return _userImage; }

    public void setName(Name n) { _name = n; }
    public void setVerified(boolean b) { _isVerified = b; }
    public void setGender(Gender g) { _gender = g; }
    public void setUserImage(byte[] b) { _userImage = b; }
}

After downloading and importing the Jackson libraries, you can turn that Json string into a User object:

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class);

Simple, right?

Passing it a data source (File, InputStream, etc..) instead of a plain old String will cause the parser to parse the Json similar to SAX parsing, which is very fast.

For XML files, we’ll just have to parse them the old-fashion way. :-)

Android bitmaps and out of memory errors

One of the things I had to learn the hard way on Android is the correct way of handling Bitmaps. And honestly, I wouldn’t have thought this is the right way if someone at Google wouldn’t have suggested it to me.

Every time you create a bitmap, either blank or from a source using BitmapFactory, the full size of that bitmap is loaded into memory. If you have a 24bit, 300×300 pixel bitmap, it will use 24*300*300 bytes, which is approximately 2Mb. Handling that kind of memory allocated in the java heap space would be slow, so the Android engineers came with the smart solution of allocating and managing that memory natively (using C).

Now, if you know just a little C, you’ll know that all memory allocations have to be done manually, and so are the de-allocations (freeing up the memory). And since java doesn’t have destructors, when would the Bitmap objects you have know how to clear that memory? They simply don’t, so you need to clear that memory manually, by calling the .recycle() method of your bitmap object. Simple yet game-changing trick.

How you keep track of the bitmaps is up to you, but (just like all of the other variables), avoid using statics. If you REALLY need global objects, extend Application and keep your stuff there.

Making your Android app look better

We all know how ugly most of the applications on the Market are. Black screens, gray buttons, white texts, all native stuff which looks good but only in the right combination. Many developers realize that, and try to do their own buttons,  which still don’t look too professional, because they simply don’t fit in. And of course, there are those who just copy some iPhone interface.

First of all, no matter what you do, if you use gradients or background images you’ll inevitably see the banding effect. Here’s an example (see the stripes?):

You can get rid of that with just two lines of code, carefully placed between super.onCreate(…) and setContentView(…) in your activity. This will activate dithering for all your activity, and increase the color range a lot:

super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.main);

I’ll let you check out the result for yourself :-)

Now, that background looks pretty cool, right? It’s simple, it’s light, and very customizable. So, how did I do it?

Xml drawables. They are powerful, customizable, good looking, light, and you can create entire interfaces using them. Let’s create  file in our drawables folder named background.xml and throw this inside:

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<gradient
android:angle=”90″
android:endColor=”#fff”
android:startColor=”#999″
android:type=”linear”
/>
</shape>

You can use it like any other drawables, and here’s an example:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/background”
>
</LinearLayout>

Simple right? Of course, you probably already knew these :-)

Now let’s create some buttons. Here are three buttons, gray, green and blue

gray:

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#F000″/>
<stroke android:width=”1px” android:color=”#BB000000″ />
<padding
android:left=”10dp”
android:top=”7dp”
android:right=”10dp”
android:bottom=”7dp”
/>
<corners
android:bottomRightRadius=”5dp”
android:bottomLeftRadius=”5dp”
android:topLeftRadius=”5dp”
android:topRightRadius=”5dp”
/>
<gradient
android:angle=”90″
android:startColor=”#777″
android:centerColor=”#777″
android:endColor=”#BBB”
android:type=”linear”
/>

</shape>

green:

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#F000″/>
<stroke android:width=”1px” android:color=”#BB000000″ />
<padding
android:left=”10dp”
android:top=”7dp”
android:right=”10dp”
android:bottom=”7dp”
/>
<corners
android:bottomRightRadius=”5dp”
android:bottomLeftRadius=”5dp”
android:topLeftRadius=”5dp”
android:topRightRadius=”5dp”
/>
<gradient
android:angle=”90″
android:startColor=”#739312″
android:centerColor=”#86a621″
android:endColor=”#9cc126″
android:type=”linear”
/>

</shape>

blue:

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#F000″/>
<stroke android:width=”1px” android:color=”#BB000000″ />
<padding
android:left=”10dp”
android:top=”7dp”
android:right=”10dp”
android:bottom=”7dp”
/>
<corners
android:bottomRightRadius=”5dp”
android:bottomLeftRadius=”5dp”
android:topLeftRadius=”5dp”
android:topRightRadius=”5dp”
/>
<gradient
android:angle=”90″
android:startColor=”#4747e0″
android:centerColor=”#5b5bcd”
android:endColor=”#6f6fcf”
android:type=”linear”
/>

</shape>

Check out how they look:

sexy, right ?

Here’s the layout I used with those buttons, which contains the text colors, shadows and everything else:

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#F000″/>
<stroke android:width=”1px” android:color=”#BB000000″ />
<padding
android:left=”10dp”
android:top=”7dp”
android:right=”10dp”
android:bottom=”7dp”
/>
<corners
android:bottomRightRadius=”5dp”
android:bottomLeftRadius=”5dp”
android:topLeftRadius=”5dp”
android:topRightRadius=”5dp”
/>
<gradient
android:angle=”90″
android:startColor=”#4747e0″
android:centerColor=”#5b5bcd”
android:endColor=”#6f6fcf”
android:type=”linear”
/>

</shape>

So, don’t be shy on using drawables, as they will make your applications a lot smaller, they scale perfectly on all screen-sizes, and adapt very well to tablets as well.

Praises from the competition

In spite of what I think of my writing skills and my apps’ descriptions, it looks like someone else thinks it’s good enough to copy them to their apps :-)

I’ve recently discovered an application on the Android market, called “Antivirus – Risk Detector” by Easy Filters, which competes with Antivirus Free. It’s nothing like my application, excepting the description. 90% of it is copied from mine (they could say they wrote it themselves, since they have some misspelled words here and there :) ), and you can check it out for yourself here. Honestly, I don’t mind it, and I really feel flattered. It’s a big deal when a company with over 1 million in total app downloads considers I’m good enough :P

So, thank you Easy Filters. I really appreciate it!

Android, drawables and paddings

I learned one important thing today: When you do layout from code and you use drawables (nine-patches or xml-defined drawables), always using setPadding only after setBackground. I know, this might be a childish thing that Android developers already know about, but it made me bang my head around the walls for 15 minutes today :-)

Explanation: nine-patches and xml-drawables already contain padding information, so calling setBackground will also override any padding you previously set.

Remember: if your padding doesn’t work, check out for background setters!

Optimizing earnings from your Android apps

As opposed to Apple’s AppStore, 65% of the apps on the Android Market are free. So, where’s the trick ? Well, most of them show ads, you (the user) view those ads and when something interesting is shown you even click them. And that’s when the developer makes money. Sometimes $0.01 per click, sometimes $3.5 per click (at least that’s my record). Usually, those ads come from Google’s AdMob, Millennial Media or ZestAdz (there are more, but these are the largest three). And most of the time, all ads on one application come from just one of these advertisers.

Also, most of the time, if an application is ran 100 times by some user, the advertisers will only server 50 ads, leaving 50 impressions useless for your pockets. And this is where AdWhirl comes in. This service will help you fill out those 50 empty impressions, by fetching ads from different advertisers to make the most of your app. Personally, I use AdMob and Millennial Media, and I consider both of them to be great services. And my revenue doubled when I switched from AdMob to AdWhirl which mediates both networks.

From a technical point of view, you still need to include all of the manifest stuff from both AdMob and Millennial (activities and permissions), and the libraries for them (including AdWhirl), but you don’t need to create or add AdViews to your layouts or Activities. All you need to do is to create (usually from code) an  AdWhirlLayout and set your publisher ID, and you’re done. And of course, here’s the official tutorial.

Switch to our mobile site