Tuesday, June 29, 2010

How to tile a background image in Android


For one of the apps I'm working on I wanted to have a nice pixel pattern tiled behind my widgets.
After a little bit of hunting around I found this tutorial, and I thought I'd clean up the lessons within and show you how.

Here's the contents of my main.xml layout file,

<?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/backrepeat"
    android:gravity="center_horizontal"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />  
</LinearLayout>



which is referenced in code in the standard way like this:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...// (rest of onCreate method continues here..)

Now note this line:

android:background="@drawable/backrepeat"


What's going on there?
.. Glad you asked!

Here's a quick screenshot of the contents of one of my drawable folders in my project:


What is this
backrepeat.xml?

Well, here's the contents of that file here:

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/scale1"
android:tileMode="repeat"
android:dither="true" />



Can you see what's going on?
Backrepeat.xml defines an instance of the BitmapDrawable class, and that class references our simple scale1.jpg, located in the drawable-hdpi folder.

Simply by adding the:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/scale1"
    android:tileMode="repeat"
    android:dither="true" />

line in bold, we are able to achieve results such as this:



Easy isn't it?

One thing to keep in mind is that you should have folders drawable-hdpi, drawable-mdpi & drawable-ldpi, you'll need to add this backrepeat.xml file and the relevant images to each of these to allow this functionality in high, medium and low dpi (dots per inch) screen sizes.

Enjoy.

Saturday, June 19, 2010

Localisation & Internationalisation on Android, the easy way


You often hear about Internationalisation (internationalization in US English, or i18n for short), and Localisation (Localization in US English, or L10n for short) in software development circles... ever wonder what that's all about?

Of course you do.

Definitions vary, but the basic idea is generally easily summarised:
  • Internationalisation is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes.
  • Localisation is the process of adapting internationalized software for a specific region or language by adding locale-specific components and translating text.

These two terms usually go hand-in-hand as the term 'globalisation'. See?
.. Course you do.


Each language is given a language code such as 'en-au' for Australian English, or 'en-us' for American English. These language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1.
But how do we use this? And how can we easily apply these concepts to our Android programming?

.. Good question!

Best practices in Android suggests defining all your string resources in a 'strings.xml' file (you're doing this already, right?) and placing that file in the 'res\values' folder in your project.

Localisation in Android is as simple as creating a new version of this file and folder, renaming the 'values' folder to 'values' + the language code you wish to support.

For example, all your Italian translations would be located in 'res\values-it\strings.xml', your Chinese translations in 'res\values-zh\strings.xml' as seen here:



If you don't happen to have a friend from whatever country you're trying to translate your app for, just use Google translate.

Once you're done your app is one simple step away from global domination.

All you need to do is copy those freshly translate strings back into a strings.xml file in Eclipse, and, as long as the folder the strings.xml file is located in has the name of 'values' + the language code you're wishing to support, magically any user who has their phone locale set to a locale that uses that language, your translated strings will be used.

No code changes are required.

Handy eh?

Sunday, June 6, 2010

My first App is now available on the Android market, free.


Aspect Ratio Calculator is a simple tool that makes it easy to calculate the dimensions to resize an image.

Great for graphic designers, or anyone resizing images for blogging, it's available in Spanish, Russian, Portuguese, Chinese & Italian.

.. And of course, English ;)