July 22nd, 2010

Go home feature with FLAG_ACTIVITY_CLEAR_TOP

Here’s a little snippet I wrote up the other day to support a Go Home feature for the app I’ve been helping with.  It sets the FLAG_ACTIVITY_CLEAR_TOP flag on the Intent along with the HomeActivity.  This flag, according to the Android docs, launches a new instance of the activity if it isn’t currently running, and closes all the activities on top of it.  So if there are any activities on top of HomeActivity they are cleared and HomeActivity is displayed to the user.  I also added a check to determine if the supplied Context is already the HomeActivity, and if so, ignore the call.

public static void goHome( Context ctx )
{
    if ( ! ( ctx instanceof HomeActivity ) )
    {
        Intent intent = new Intent( ctx, HomeActivity.class );
        intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
        ctx.startActivity( intent );
    }
}

July 1st, 2010

Using layout include tag with Location proof of concept

I’ve been prototyping some code using the location api and not only have I learned some new things about this but also worked in the layout ‘include’ tag to minimize the amount of redundency in the layout file, and the Android Java code.  Include allows you to include one layout file in another, there’s an article about it on the Android dev site.  One minor speed bump I ran into was that I didn’t see an immediate way to access the resuseable portion of the layout file via a findById call.  After a little hacking around, the answer was pretty easy.

read the rest of this entry… »

June 28th, 2010

Multiple calls to addPreferencesFromResource from PreferenceActivity

Just found this out, maybe it’s obvious, but something new for me.  When you create a PreferenceActivity you call addPreferencesFromResource with the name of the resource file defining your preferences to display in the activity.  You can make multiple calls to the addPreferencesFromResource method; this gives your Android application a way to show preferences that may be specific to that component of your app by breaking your preferences up into multiple resource files and aggregating them as needed.

I’d think if this functionality was used you’d want one screen that would aggregate all the preferences (maybe a Dashboard or Home screen), but individual activities could call other preferences activities that would further filter the preferences specific to that activity.

Just another option that you may find useful in your app….cheers!

June 17th, 2010

Android Item Resource has a Name and Type

Here’s a handy trick to use instead of constants in your code.  It uses the Item resource tag to define application specific constants which are located in a resource XML file.

For example, given the following XML located in a file in the res/values directory

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <item name="host" type="setting">10.0.2.2</item>
 <item name="port" type="setting">3000</item>
</resources>

I can now use the following Java syntax within an Android Activity:

String host = getString( R.setting.host );
int port    = getResources().getInteger( R.setting.port );

This gives me a scheme to have a common place with ‘types’ to define constants. I’ve just barely started using it, but it seems like a nice clean way of handling these ‘magic’ values without using static final variables.

June 10th, 2010

ArrayAdapter and AlertDialog for single choice items

I thought this was pretty cool…I used the AlertDialog.Builder, an ArrayAdapter along with the View Holder Android UI pattern to build an AlertDialog containing a list of choices with images for each entry.

read the rest of this entry… »

June 5th, 2010

Cropping images from a png file in Android

So I’ve never tried to crop single images from a multi-image bitmap ( like these images ), but after some tweeking I ended up with the following code snippet, I’m sure it’s not the only way, but it seems to work out pretty well.

private Drawable getImg( int idx )
{
    InputStream is = getResources().openRawResource( R.raw.myicons );
    BitmapDrawable bm = new BitmapDrawable( is );
    try {
        is.close();
    } catch (IOException e) { /** noop, stream is closed **/ }
    BitmapDrawable img = new BitmapDrawable( Bitmap.createBitmap( bm.getBitmap(), 22 * idx, 0, 22, 22 ) );
    img.setBounds( 0, 0, 40, 40 );

    return img;
}

Of course, you’ll have to supply your own myicons.png in the /res/raw directory and a corresponding size (mine is 22).  You’ll also want to change the code so the BitmapDrawable “bm” is only loaded once, most likely in a constructor, then used multiple times from the above getImg method.

June 4th, 2010

setCompoundDrawable to add Drawables to TextView

Finally trying to get a couple new posts up, have a bunch of material just not a lot of free time to get ‘em posted.  Here’s a quick and easy one.

I was using the setCompoundDrawable method for a TextView to set an image to the left of some text, I was hoping it was going to be as easy as loading the drawable and calling

txtVw.setCompoundDrawables( img, null, null, null );

Unfortunately nothing showed up, and I had no internet connection at the time, so I was left to fumble around trying a bunch of different things…nothing worked.  Finally, after getting to work and looking at some existing code from a co-worker, I found out that using setBounds will define a bounding rectangle around the image.  So the final lines of code look like…

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );

UPDATE: See comments to make this even easier, thanks Romain Guy and Nerdrow

February 26th, 2010

Polish and Release Early Release Often

CAUTION: Meandering editorial follows…

I’ve been trying for a week to write a post about the recent demo/Android talk I did (along with Mike Koss and Alberto Fonseca) with the Google Technical Users Group in Seattle (huge thanks to Mike Koss for setting this up!) earlier this month.  There’s a comment I made in this talk that has been hounding me ever since.  Since then I’ve wanted to expand on it and make it clear what my position is.  At about 1:08 into the video, I made it sound like getting something out the door is more important than making sure it’s polished.  I strongly believe in the release early and release often mentality.  I have been on far to many projects that have belabored releasing because of some assumed polish that needed to be done.  Whether it was that one more magical feature, or that one more must fix bug, there was always something holding back the release date.  I certainly respect those companies that have the resources and skills to pull the “make sure it’s polished” standard…one of these companies is of course Apple.  They have an awesome sense for design and what customers want, and they have the resources to develop in house and behind closed doors….I don’t…I don’t even have the resources to hire a decent graphic designer or QA person, and if I spent all my time worrying if a certain icon looked pleasing or if that one bug was a show stopper, I’d never, ever get anything out the door.  So as a single developer trying to make the simplest product, I gotta just put it out there and see how it does.  Maybe it’ll suck?  Or, maybe for whatever reason it won’t gain traction.  But wallowing in “does it have enough polish” land doesn’t answer anything, so I choose to get it out the door, and the Android platform allows me to quickly do this.

Maybe I’ll stumble upon that one app that is an overnight success and is so nicely polished that everyone wants to pay me 99 cents for it, but I’m not betting on it…and that statement doesn’t make me feel any less talented then the person who does make that overnight success app.  That person was just luckier to came across (for whatever reason) that one idea that they stuck with and where able to pull off!  And I hugely congratulate them on their success!

Hope you enjoy the little talk we had at Startpad….here’s the link if the embed doesn’t work

February 26th, 2010

Make it small and simple

I’m a huge fan of David Heinemeier Hansson and 37signals, he was recently at Stanford’s Entrepreneur Thought Leaders Seminar Series doing a talk called “Unlearning Your MBA”. There’s a load of wisdom for entrepreneurs and it’s also very entertaining.

February 25th, 2010

MediaDroid YouTube demo video

Well here’s my very first YouTube video…it’s a demo of MediaDroid. Certainly not perfect, but I think it gets the job done and I’m stick’n with it!

Here’s the link in case the embed doesn’t work