File Browser

Posted in News on May 15th, 2008 by matt

As a learning exercise, to get used to have a go at putting an android UI together I (matt) have been working on a File Browser.

Here’s a few screen shots of the nearly finished article:

Failed, but hey maybe next time

Posted in News on May 12th, 2008 by matt

No luck in the Android contest, but hey as we only had like 3 weeks to learn the platform and write the application, it wasn’t really unexpected. Congratulations to everyone who did make the top 50.
Looking forward to seeing some of the entries…

Andy Rubin demos android on a 300Mhz touch screen device

Posted in News on May 7th, 2008 by matt

Browsing about and found this http://blip.tv/file/698098/

Dos style file filter

Posted in General Java on May 7th, 2008 by matt

Can’t remember which forum I found this on, but I’m pretty sure it wasn’t licensed, if I am wrong then please contact me.

The following class can be used with ListFiles to filter the files returned using dos style * and ? wildcards.

        public class RegexFilenameFilter implements FilenameFilter {
                String _regex = null; // setting the filter regex to null causes any

                // name to be accepted (same as ".*")
                public RegexFilenameFilter() {
                }

                /**
                 * Set the filter from a wildcard expression as known from the windows
                 * command line ("?" = "any character", "*" = zero or more occurances of
                 * any character")
                 *
                 * @param sWild
                 *            the wildcard pattern
                 *
                 * @return this
                 */

                public RegexFilenameFilter setWildcard(String sWild) {
                        _regex = wildcardToRegex(sWild);
                        Pattern.compile(_regex);
                        return this;
                }

                /**
                 * Set the regular expression of the filter
                 *
                 * @param regex
                 *            the regular expression of the filter
                 *
                 * @return this
                 */

                public RegexFilenameFilter setRegex(String regex)
                                throws java.util.regex.PatternSyntaxException {
                        _regex = regex.toLowerCase();
                        Pattern.compile(_regex); // throw PatternSyntaxException if the
                        // pattern is not valid
                        return this;
                }

                /**
                 * Tests if a specified file should be included in a file list.
                 *
                 * @param dir
                 *            the directory in which the file was found.
                 *
                 * @param name
                 *            the name of the file.
                 *
                 * @return true if and only if the name should be included in the file
                 *         list; false otherwise.
                 */

                public boolean accept(File dir, String name) {
                        boolean bAccept;

                        if (_regex == null)
                                bAccept = true;
                        else
                                bAccept = name.toLowerCase().matches(_regex);

                        return bAccept;
                }

                /**
                 * Converts a windows wildcard pattern to a regex pattern
                 *
                 * @param wild -
                 *            Wildcard patter containing * and ?
                 *
                 * @return - a regex pattern that is equivalent to the windows wildcard
                 *         pattern
                 */

                private String wildcardToRegex(String wild) {
                        if (wild == null)
                                return null;

                        StringBuffer buffer = new StringBuffer();

                        char[] chars = wild.toCharArray();

                        for (int i = 0; i < chars.length; ++i) {
                                if (chars[i] == ‘*’)
                                        buffer.append(".*");
                                else if (chars[i] == ‘?’)
                                        buffer.append(".");
                                else if ("+()^$.{}[]|".indexOf(chars[i]) != -1)
                                        buffer.append(\\).append(chars[i]);
                                else
                                        buffer.append(chars[i]);
                        }

                        return buffer.toString().toLowerCase();
                }
        }
 

create an isntance, call setWidcard, then pass to listFiles.

Android Activity Designer

Posted in Other Android Stuff on April 30th, 2008 by matt

Found this graphical Activity designer for Android. I will let you know what I think..
http://droiddraw.org/

Working with OptionMenus

Posted in News on April 27th, 2008 by matt

Each activity can have it’s own menu defined. Here’s a quick info on getting your menu up and running.

OptionsMenu event cycle

  1. onCreateOptionsMenu(Menu menu) - The point at which you should add all the items you need into the menu. Call the super inherited method to have the menu populated with the default items.
  2. onPrepareOptionsMenu(Menu menu) - The point at which you can enable / disable and dynamic content, etc to respond to the state of the activity.
  3. onOptionsMenuClosed(Menu menu) - called when the menu is closed.

When items are added to the Menu in OnCreateOptionsMenu you can either pass a runnable in at that point to associate with the menu item, or alternatively you can override the onOptionsItemSelected to provide a central place where you can call out to methods that the menu items invoke.

Other OptionsMenu methods

  • openOptionsMenu() - Opens the OptionsMenu
  • closeOptionsMenu() - Closes the OptionsMenu if open.

SD Cards and the Emulator

Posted in Code snippets on April 25th, 2008 by matt

From a command prompt / terminal navigate to the android sdk’s tools folder.

Resetting the emulator

To reset the emulator and wipe all the stored data, returning as it were to factory settings run:

emulator -wipe-data

Create a blank SD card image:

mksdcard <size> <filename>

e.g. mksdcard 512M halfagig.sd

will create a 512 MegaByte sdcard image called halfgig.sd.

Mounting an SD Card in the emulator:

emulator -sdcard <filepathandname>

This will mount the card under the path /sdcard

Mounting an SD Card image under Linux:

mount -o loop [filename] [/media/mountpoint]

Background Intents (non-visual)

Posted in Tutorials on April 24th, 2008 by matt

Intents have multiple uses on the android platform and this fact makes them inherently confusing. In the single mechanism a variety of uses are implemented and this is only too apparent when you look at the Intent class constructor. When delving into the intents with Craig, he wondered why a nice Intent class hierarchy had not been implement to cleanly segment their uses.

This article addresses the use of Intents for sending and receiving messages in the background, for those circumstances, where you do not wish any impact on the users experience and will usually respond the notification asynchronously.

Read more »

Syntax Highlighting

Posted in News on April 23rd, 2008 by matt

The droidapps blog is using Dean Lee’s Syntax Highlighting plug in for wordpress. http://www.deanlee.cn/wordpress/code_highlighter_plugin_for_wordpress/

Code is simply wrapped with <pre lang=”java”> </pre> and the plugin does the rest. It will syntax highlight various languages including javascript, php, c sharp

Android Developer Challenge

Posted in News on April 23rd, 2008 by matt

Craig and I (no relation despite the common surname) submitted an application into the Google Android Developer challenge. We only came across android about three weeks ago, and as we both have a full time job and families, it was a bit of a push to learn as much as we could about the platform and code an app in two weeks. It was a bit close to the wire, and we took full advantage of the time difference on day now known as “can’t type tuesday”. At about 3:30am we finally submitted the application, a Podcast down loader and player.

So the long wait ’till May 5th continues…. a 1 in 35 chance isn’t that bad…. is it?

How to capture key pad presses in an Activity.

Posted in Code snippets on April 23rd, 2008 by matt

To capture the keypad presses and any other keys on the phone…
Override the Activities OnKeyDown and compare the keycode against one of the constants in KeyEvent.KEYCODE_*

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent ev) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                        // do something
                        return true;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                        // do something
                        return true;
                default:
                                return super.onKeyDown(keyCode, ev);
                }
        }
 

A couple of things to note are, returning true informs the caller that you have handled the keypress and it does not need to propagate to other views. Don’t forget to return the result of super.onKeyDown as the last resort or you’ll find out the default handling of keys no longer gets processed for your activity. (e.g. the back button will no longer navigate back through the Activity stack)