Tips & Tricks: Screen sizes

Hi Android developers,

Last week was hectic, we needed to reinstall and reconfigure the backend server of Perka’s File stash.

This week we will take a little twist and we will talk about a topic that is more focused on design: Screen sizes.

When designing Android apps the best practice is to use Density-independent pixel (dp) as measure. A dp is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a “medium” density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160).

Another best practice is to use wrap_content and fill_parent in XML so the layout will scale automagically. Also a RelativeLayout is a good practice, it will keep the distances the same even if the screen is bigger or smaller.

Also keep in mind of using layout and orientation qualifiers, which can be added to the /res/ directory. This will make it easy to make to make a different layout for another screen size and/or orientation.

For images used in for the different screens, you might want to use draw9patch to make image.9.png files. These files are processed by the tool and you can assign how it will be stretched.

Another good practice is to use Fragments, this will allow you to reuse designs in different layouts. You can combine the fragments in a layout or just use 1 fragment as layout.

Extended information can be found at: Supporting Multiple Screen Sizes and Android Training Multiscreen

Kind regards,
PullesSon – Android

Tips & Tricks: AsyncTask.

Hi Android developers,

This week in tips and tricks: AsyncTask.

A AsyncTask is a class that provides you an easy interface to create a new thread. It is recommended to use it by background tasks, like downloading through a internet connection.
From Honeycomb (SDK API level 11) it’s even mandatory to use Asynctask’s for internet connection downloads and if it’s not done it will force close the application. So it will be also for Ice Cream Sandwich.
Besides it’s mandatory it’s also really handy. You will take pressure from the UI Thread and it will be processed faster and parallel to the UI Thread.

You have to start with:

private class DownloadFilesTask extends AsyncTask<void, void, void>

There are five methods to implement which are:
onPreExecute:
The first method called in the AsyncTask, called on the UI thread. In this method you usually prepare the progressbar, if needed.

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

doInBackground:
This is the method where is all the action. This method executes the time consuming tasks and publish the task progress, executed in background thread.

@Override
protected String doInBackground(String... params) {
    // perform Long time consuming operation
    return null;
}

onProgressUpdate
Method that updates the progress of the AsyncTask, run on the UI thread. This is where you update the progressbar.

@Override
protected void onProgressUpdate(String... progress) {
    mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

onPostExecute
The final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask, like removing the progressbar.

@Override
protected void onPostExecute(String unused) {
    try {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    } catch (Exception e){
        Log.e("APP_NAME","Unknown exception caught: " + e.getMessage());
    }
}

onCancelled
This method gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask.

@Override
protected void onCancelled() {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}

To execute it just use:

new DownloadFilesTask().execute(param1, param2);

As you can see the AsyncTask is easy to implement and the methods are really easy to use and understand. It is highly recommended to use AsyncTask for task that doesn’t have to update the UI Thread.

We use AsyncTask in Let Me Android That For You and Perka’s File Stash.

For more information: http://developer.android.com/reference/android/os/AsyncTask.html

Regards,
PullesSon – Android

Perka’s File Stash – update

Perka’s File Stash is updated with the: Flash zip option to flash CWM zips.
Long press on the zip file -> choose: Flash zip -> Wait untill it’s downloaded -> confirm the flash, if the file is already downloaded, just confirm the flash.